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 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.
In one of my modules (PSUtil) I have functions that emulate this functionality:
2,3,1,4 | sort | join "."
However I think this would make sense as part of the default command set, as virtually every user of PowerShell has to handle strings.
Weighing the scales
Advantages
- More convenient to use for end user on the pipeline
- Additional suitable features can be added through parameters (See implementation proposal below)
Disadvantages
- Clobber list of commands available by default with more commands.
On Implementation
I am willing to implement all commands involved, their tests and documentation, all by myself, if this feature is deemed worthwhile.
Proposed commands & features
Add-String (Alias: 'wrap')
Does not have an operator equivalent. Command that allows you to easily add to a string on the pipeline.
Example
PS> 1..4 | wrap '"' '"'
"1"
"2"
"3"
"4"
Parameters
- InputString (
string[] | Pipeline), the string(s) being added to
- Before (
string), the string to add before the input
- Behind (
string), the string to add behind the input
- PadLeft (
char), character to pad left with
- PadRight (
char), character to pad right with
- PadWidth (
int), up to how many characters the string should be padded with
Format-String (Alias: 'format')
Equivalent to -f. Command that allows you to use the format operator on the pipeline.
Example
PS> 1..4 | format "{0:N2} - {1:D3}" -Count 2
1,00 - 002
3,00 - 004
Parameters
- InputObject (
object | Pipeline), objects to be formatted
- Format (
string), the format definition
- Count (
int), the number of items to store up before formating them in bulk (optional, default 1)
Get-Substring
Allows trimming and picking a substring from specified strings.
Example
PS> "abc def ghi" | substring -trim "abi"
c def gh
PS> "abc def ghi" | substring 2 4
c de
Parameters
- InputString (
string[] | Pipeline), the strings to pick from
- Trim (
string), what characters to trim
- TrimStart (
string), what characters to trim at the start of the string
- TrimEnd (
string), what characters to trim at the end of the string
- Start (
int), the start index to pick the substring from
- Length (
int), how long a substring to pick
Join-String (Alias: 'join')
Equivalent to -join. Command that allows you to join items on the pipeline
Example
PS> 1..4 | join ","
1,2,3,4
PS> 1..4 | join "," -Count 2
1,2
3,4
Parameters
- InputString (
string[] | Pipeline), the strings to join
- Separator (
string), what string to join them with. Defaults to ([System.Environment]::NewLine)
- Count (
int), the number of items to join together. (Defaults to all items)
Set-String ('replace')
Equivalent to the -replace operator and the .Replace() string method
Example
PS> "abc def ghi" | replace "d\w+" "zzz"
abc zzz ghi
PS> "abc def ghi" | replace "d\w+" { 1..4 | join "." }
abc 1.2.3.4 ghi
PS> "abc (def) ghi" | replace "(def)" "def" -DoNotUseRegex
abc def ghi
Parameters
- InputString (
string[] | Pipeline), the strings to replace within
- OldValue (
string), what sequence to replace
- NewValue (
object), what to replace with. Can be a string or a scriptblock
- DoNotUseRegex (
switch), switches from regex replace to using the string method Replace()
- Options (
RegexOptions), the regex options to use on replace, defaults to IgnoreCase
Split-String ('split')
Equivalent to the -split operator and the .Split() string method
Example
PS> "abc def ghi" | split " d\w+ "
abc
ghi
PS> "abc def ghi | split " d\w+ " -DoNotUseRegex
abc
ef
ghi
Parameters
- InputString (
string[] | Pipeline), the strings to split
- Separator (
object), what to split with.
- DoNotUseRegex (
switch), switches from regex split to using the string method Split()
- Options (
RegexOptions), the regex options to use on split, defaults to IgnoreCase
- Count (
int), the maximum number of items to split into (equivalent to -split ".",2)
Concluding
I 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 :)
Opinions / comments / refinements, anybody?
2018-04-27 - Updated parameter names, replaced Remove-String (trim) with Get-SubString
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:
Not 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.
In one of my modules (
PSUtil) I have functions that emulate this functionality:However I think this would make sense as part of the default command set, as virtually every user of PowerShell has to handle strings.
Weighing the scales
Advantages
Disadvantages
On Implementation
I am willing to implement all commands involved, their tests and documentation, all by myself, if this feature is deemed worthwhile.
Proposed commands & features
Add-String (Alias: 'wrap')
Does not have an operator equivalent. Command that allows you to easily add to a string on the pipeline.
Example
Parameters
string[]| Pipeline), the string(s) being added tostring), the string to add before the inputstring), the string to add behind the inputchar), character to pad left withchar), character to pad right withint), up to how many characters the string should be padded withFormat-String (Alias: 'format')
Equivalent to
-f. Command that allows you to use the format operator on the pipeline.Example
Parameters
object| Pipeline), objects to be formattedstring), the format definitionint), the number of items to store up before formating them in bulk (optional, default 1)Get-Substring
Allows trimming and picking a substring from specified strings.
Example
Parameters
string[]| Pipeline), the strings to pick fromstring), what characters to trimstring), what characters to trim at the start of the stringstring), what characters to trim at the end of the stringint), the start index to pick the substring fromint), how long a substring to pickJoin-String (Alias: 'join')
Equivalent to
-join. Command that allows you to join items on the pipelineExample
Parameters
string[]| Pipeline), the strings to joinstring), what string to join them with. Defaults to([System.Environment]::NewLine)int), the number of items to join together. (Defaults to all items)Set-String ('replace')
Equivalent to the
-replaceoperator and the.Replace()string methodExample
Parameters
string[]| Pipeline), the strings to replace withinstring), what sequence to replaceobject), what to replace with. Can be a string or a scriptblockswitch), switches from regex replace to using the string methodReplace()RegexOptions), the regex options to use on replace, defaults toIgnoreCaseSplit-String ('split')
Equivalent to the
-splitoperator and the.Split()string methodExample
Parameters
string[]| Pipeline), the strings to splitobject), what to split with.switch), switches from regex split to using the string methodSplit()RegexOptions), the regex options to use on split, defaults toIgnoreCaseint), the maximum number of items to split into (equivalent to-split ".",2)Concluding
I 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 :)
Opinions / comments / refinements, anybody?
2018-04-27- Updated parameter names, replacedRemove-String (trim)withGet-SubString