Why doesn't the split() function in Powershell match the whole given String?

Viewed 64

I wonder why Powershell's .split() function method isn't giving me the expected result. Actually I want to split a given String at the occurence of CC= but the function seems to split my String at every occurence of C and =.

For example:

$var = "randomStinng,XX=someContent,CC=moreCotent,CC=end"
$var.Split("CC=")

randomStinng,XX
some
ontent,


more
otent,


end

I try to avoid using -split since I with split() expressions like $var.Split("CC=")[3] are possible.

1 Answers

The -split operator uses regular expressions, whilst the .Split() method does not. Does the following work for you?

$var = "randomStinng,XX=someContent,CC=moreCotent,CC=end"
$var -split "CC="

Edit:

You can use -split like you want it too:

($var -split "CC=")[3]
Related