Skip to content

New Feature: Commands for string methods & operators on the pipeline #6697

@FriedrichWeinmann

Description

@FriedrichWeinmann

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Enhancementthe issue is more of a feature request than a bugResolution-No ActivityIssue has had no activity for 6 months or moreWG-Cmdlets-Utilitycmdlets in the Microsoft.PowerShell.Utility module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions