1

I'm trying to split on the ' (' (space followed by open brace)

I want to keep the name of the subscription but discard everything else after

$string = 'SUB-AAD (e775'
$string.tostring().split('`` (')[0]

This works ok


$string = 'Active Directory (758239'
$string.tostring().split('`` (')[0]

This only shows 'Active', it seems to be splitting on the space too, even though I have not specified this.

Maybe I can find an instance of ' (' and discard everything after that position, but not sure how to.

Aziz
  • 283
  • 1
  • 14
  • Steven's answer explains it well, but in short: your `.split('\`\` (')` call was interpreted as `.split([char[]] '\`\` (')`, meaning that _any one_ of the characters served as a separator, not the _string as a whole_ (which couldn't have been expected to match, due to the extraneous `\`` chars.). – mklement0 Jun 01 '21 at 17:55

2 Answers2

5

You can use PowerShell's Built in -split operator like:

($String -split " \(")[0]

Above is preferred considering it will work around differences in .Net functionality .Net Framwork versus .Net Core, where the overloads are ordered differently.

Also, and less well known, you can use the other overloads to get .Split() to split on multiple characters instead of each character:

$string.split([string[]]' (', [StringSplitOptions]::None)[0]

Above the inclusion of [StringSplitOptions] appears to be required so the overloads will resolve properly. Attempting to cast the delimitator only will result in an error.

Note: the available overloads in Windows PowerShell:

OverloadDefinitions
-------------------
string[] Split(Params char[] separator)
string[] Split(char[] separator, int count)
string[] Split(char[] separator, System.StringSplitOptions options)
string[] Split(char[] separator, int count, System.StringSplitOptions options)
string[] Split(string[] separator, System.StringSplitOptions options)
string[] Split(string[] separator, int count, System.StringSplitOptions options)

And in PowerShell Core:

OverloadDefinitions
-------------------
string[] Split(char separator, System.StringSplitOptions options)
string[] Split(char separator, int count, System.StringSplitOptions options)
string[] Split(Params char[] separator)
string[] Split(char[] separator, int count)
string[] Split(char[] separator, System.StringSplitOptions options)
string[] Split(char[] separator, int count, System.StringSplitOptions options)
string[] Split(string separator, System.StringSplitOptions options)
string[] Split(string separator, int count, System.StringSplitOptions options)
string[] Split(string[] separator, System.StringSplitOptions options)
string[] Split(string[] separator, int count, System.StringSplitOptions options)

You can check this answer and the surrounding discussion for more information. You can also find a very robust explanation of the pitfalls of using .Split() in this answer.

Note: You shouldn't have to run the .ToString() method on something that's already a string. So I removed that from the above examples.

Steven
  • 6,817
  • 1
  • 14
  • 14
  • @Aziz, thank your for accepting my answer, but can I suggest accepting Steven's instead? While the `-replace` solution in my answer may _ultimately_ be simplest, Steven's answer addresses your question _as asked_, and future readers may be more interested in the ins and outs of `.Split()` vs. `-split`. – mklement0 Jun 02 '21 at 19:22
4

To complement Steven's helpful answer, which sensibly advocates for preferring PowerShell's regex-based -split operator to the .NET [string] type's System.String.Split() method.

As an alternative to -split, consider this convenient idiom for dropping a suffix from a string, based on the regex-based -replace operator:

PS> 'Active Directory (758239' -replace ' \(.*'
Active Directory

Note: Due to not passing a substitution-text argument, '' is effectively used, resulting in the effective removal of the match from the output string; that is, -replace ' \(.*' is implicitly the same as -replace ' \(.*', ''

mklement0
  • 382,024
  • 64
  • 607
  • 775