Reputation: 293
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.
Upvotes: 1
Views: 2081
Reputation: 7067
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.
Upvotes: 5
Reputation: 438198
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 ' \(.*', ''
Upvotes: 4