Title: New Feature: Commands for string methods & operators on the pipeline · Issue #6697 · PowerShell/PowerShell · GitHub
Open Graph Title: New Feature: Commands for string methods & operators on the pipeline · Issue #6697 · PowerShell/PowerShell
X Title: New Feature: Commands for string methods & operators on the pipeline · Issue #6697 · PowerShell/PowerShell
Description: Introduction One of the things I have seen that always bugged me in PowerShell was the fact, that string operators cannot be used on a pipeline. A simple example: (2,3,1,4 | Sort-Object) -join "." Not convenient, especially for interacti...
Open Graph Description: Introduction One of the things I have seen that always bugged me in PowerShell was the fact, that string operators cannot be used on a pipeline. A simple example: (2,3,1,4 | Sort-Object) -join "." ...
X Description: Introduction One of the things I have seen that always bugged me in PowerShell was the fact, that string operators cannot be used on a pipeline. A simple example: (2,3,1,4 | Sort-Object) -join &quo...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/6697
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"New Feature: Commands for string methods \u0026 operators on the pipeline","articleBody":"# Introduction\r\nOne of the things I have seen that always bugged me in PowerShell was the fact, that string operators cannot be used on a pipeline. A simple example:\r\n```powershell\r\n(2,3,1,4 | Sort-Object) -join \".\"\r\n```\r\nNot convenient, especially for interactive console use where you often need to go back and add the braces. The default string operators obviously aren't cmdlets and won't work on the pipeline - instead they are fast.\r\n\r\nIn one of my modules (`PSUtil`) I have functions that emulate this functionality:\r\n```powershell\r\n2,3,1,4 | sort | join \".\"\r\n```\r\nHowever I think this would make sense as part of the default command set, as virtually every user of PowerShell has to handle strings.\r\n\r\n# Weighing the scales\r\n## Advantages\r\n\r\n - More convenient to use for end user on the pipeline\r\n - Additional suitable features can be added through parameters (See implementation proposal below)\r\n\r\n## Disadvantages\r\n\r\n - Clobber list of commands available by default with more commands.\r\n\r\n# On Implementation\r\n\r\nI am willing to implement all commands involved, their tests and documentation, all by myself, if this feature is deemed worthwhile.\r\n\r\n# Proposed commands \u0026 features\r\n## Add-String (Alias: 'wrap')\r\n\r\nDoes not have an operator equivalent. Command that allows you to easily add to a string on the pipeline.\r\n\r\n### Example\r\n```powershell\r\nPS\u003e 1..4 | wrap '\"' '\"'\r\n\r\n\"1\"\r\n\"2\"\r\n\"3\"\r\n\"4\"\r\n```\r\n\r\n### Parameters\r\n\r\n - InputString (`string[]` | Pipeline), the string(s) being added to\r\n - Before (`string`), the string to add before the input\r\n - Behind (`string`), the string to add behind the input\r\n - PadLeft (`char`), character to pad left with\r\n - PadRight (`char`), character to pad right with\r\n - PadWidth (`int`), up to how many characters the string should be padded with\r\n\r\n## Format-String (Alias: 'format')\r\n\r\nEquivalent to `-f`. Command that allows you to use the format operator on the pipeline.\r\n\r\n### Example\r\n```powershell\r\nPS\u003e 1..4 | format \"{0:N2} - {1:D3}\" -Count 2\r\n\r\n1,00 - 002\r\n3,00 - 004\r\n```\r\n\r\n### Parameters\r\n\r\n - InputObject (`object` | Pipeline), objects to be formatted\r\n - Format (`string`), the format definition\r\n - Count (`int`), the number of items to store up before formating them in bulk (optional, default 1)\r\n\r\n## Get-Substring\r\n\r\nAllows trimming and picking a substring from specified strings.\r\n\r\n### Example\r\n\r\n```powershell\r\nPS\u003e \"abc def ghi\" | substring -trim \"abi\"\r\n\r\nc def gh\r\n\r\nPS\u003e \"abc def ghi\" | substring 2 4\r\n\r\nc de\r\n```\r\n\r\n### Parameters\r\n\r\n - InputString (`string[]` | Pipeline), the strings to pick from\r\n - Trim (`string`), what characters to trim\r\n - TrimStart (`string`), what characters to trim at the start of the string\r\n - TrimEnd (`string`), what characters to trim at the end of the string\r\n - Start (`int`), the start index to pick the substring from\r\n - Length (`int`), how long a substring to pick\r\n\r\n## Join-String (Alias: 'join')\r\n\r\nEquivalent to `-join`. Command that allows you to join items on the pipeline\r\n\r\n### Example\r\n```powershell\r\nPS\u003e 1..4 | join \",\"\r\n\r\n1,2,3,4\r\n\r\nPS\u003e 1..4 | join \",\" -Count 2\r\n\r\n1,2\r\n3,4\r\n```\r\n\r\n### Parameters\r\n\r\n - InputString (`string[]` | Pipeline), the strings to join\r\n - Separator (`string`), what string to join them with. Defaults to `([System.Environment]::NewLine)`\r\n - Count (`int`), the number of items to join together. (Defaults to all items)\r\n\r\n## Set-String ('replace')\r\n\r\nEquivalent to the `-replace` operator _and_ the `.Replace()` string method\r\n\r\n### Example\r\n```powershell\r\nPS\u003e \"abc def ghi\" | replace \"d\\w+\" \"zzz\"\r\n\r\nabc zzz ghi\r\n\r\nPS\u003e \"abc def ghi\" | replace \"d\\w+\" { 1..4 | join \".\" }\r\n\r\nabc 1.2.3.4 ghi\r\n\r\nPS\u003e \"abc (def) ghi\" | replace \"(def)\" \"def\" -DoNotUseRegex\r\n\r\nabc def ghi\r\n```\r\n\r\n### Parameters\r\n\r\n - InputString (`string[]` | Pipeline), the strings to replace within\r\n - OldValue (`string`), what sequence to replace\r\n - NewValue (`object`), what to replace with. Can be a string or a scriptblock\r\n - DoNotUseRegex (`switch`), switches from regex replace to using the string method `Replace()`\r\n - Options (`RegexOptions`), the regex options to use on replace, defaults to `IgnoreCase`\r\n\r\n## Split-String ('split')\r\n\r\nEquivalent to the `-split` operator _and_ the `.Split()` string method\r\n\r\n### Example\r\n\r\n```powershell\r\nPS\u003e \"abc def ghi\" | split \" d\\w+ \"\r\n\r\nabc\r\nghi\r\n\r\nPS\u003e \"abc def ghi | split \" d\\w+ \" -DoNotUseRegex\r\n\r\nabc\r\n\r\nef\r\nghi\r\n```\r\n\r\n### Parameters\r\n\r\n - InputString (`string[]` | Pipeline), the strings to split\r\n - Separator (`object`), what to split with.\r\n - DoNotUseRegex (`switch`), switches from regex split to using the string method `Split()`\r\n - Options (`RegexOptions`), the regex options to use on split, defaults to `IgnoreCase`\r\n - Count (`int`), the maximum number of items to split into (equivalent to `-split \".\",2`)\r\n\r\n# Concluding\r\n\r\nI believe these commands to improve the interactive console experience of most users, without having significant drawbacks and am perfectly willing to do the implementation myself, pending the approval of the court of public opinion :)\r\n\r\nOpinions / comments / refinements, anybody?\r\n\r\n`2018-04-27` - Updated parameter names, replaced `Remove-String (trim)` with `Get-SubString`","author":{"url":"https://github.com/FriedrichWeinmann","@type":"Person","name":"FriedrichWeinmann"},"datePublished":"2018-04-21T21:51:49.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":20},"url":"https://github.com/6697/PowerShell/issues/6697"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:1bab10e7-c443-4a83-f706-9c9feea8215f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C664:103DBC:2739C3D:34AC555:6A59B334 |
| html-safe-nonce | 1bc547fdf7b852671dd1799b4d61586bcf0326fe82b332039b5f7615cbb12062 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNjY0OjEwM0RCQzoyNzM5QzNEOjM0QUM1NTU6NkE1OUIzMzQiLCJ2aXNpdG9yX2lkIjoiNDU5ODYyMzI1MDg0MzAyMjEzMiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | bdf73a543cec545c218c3a792b823a3a96436278203ceadf133e74547fd8479a |
| hovercard-subject-tag | issue:316528424 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/6697/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f556671c8d4ad58e8b9e4a1278e0287b912b0f88593178aed12750a2ffcb9882/PowerShell/PowerShell/issues/6697 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f556671c8d4ad58e8b9e4a1278e0287b912b0f88593178aed12750a2ffcb9882/PowerShell/PowerShell/issues/6697 |
| og:image:alt | Introduction One of the things I have seen that always bugged me in PowerShell was the fact, that string operators cannot be used on a pipeline. A simple example: (2,3,1,4 | Sort-Object) -join "." ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | FriedrichWeinmann |
| hostname | github.com |
| expected-hostname | github.com |
| None | ba3976babb66479b1c943a8edc0777d96157da48fadc0161f9ddb219deee8353 |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 49609581 |
| octolytics-dimension-repository_nwo | PowerShell/PowerShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49609581 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PowerShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | ab680789ae4a316cdaf0d5a292a1760140931cc4 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width