This post is to summarize three ways to split a string in PowerShell.
In the following example
$t = “1000ABC2000abc"
[regex]$rx1 = “\d+”
[regex]$rx2 = “ABC"
- Using a regex object’s split method
- In this method, the regex works and the match is case-sensitive
PS > $rx1.split($t)ABC
abcPS > $rx2.split($t)
1000
2000abc
- In this method, the regex works and the match is case-sensitive
- Using a string object’s split method
- In this method, the regex does not work and the match is case-sensitive
PS > $t.split($rx1)
1000ABC2000abcPS > $t.split($rx2)
1000
2000abc
- In this method, the regex does not work and the match is case-sensitive
- Using a string object’s split operator
- In this method, the regex works and the match is case-insensitive
PS > $t -split $rx1ABC
abcPS > $t -split $rx2
1000
2000
- In this method, the regex works and the match is case-insensitive