Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell long string with "`n" appears fine in console but get .count = 1

Tags:

powershell

I'm manipulating a test where I use multiple .replace commands to format the text.

For example

$str="hello%worldZZZniceZZZtoZZZmeet"
$str.Replace("%","`n").replace("ZZZ","`n")

Output in the console is good but I need to iterate each line. problem is $str.count = 1 meaning powershell looks at this string still as one line even when it shows up good in the console. any idea?

Edit:

If I output the string to file and then read the file it does read with new lines but there's a better way I'm sure instead of outputting to file and then reading it back again

like image 353
Shahar Avatar asked Dec 22 '22 14:12

Shahar


1 Answers

Sounds like you're looking to split the string and get an array as result:

$str = "hello%worldZZZniceZZZtoZZZmeet"
$str -split "%|ZZZ"
($str -split "%|ZZZ").count # => 5

Since the operator is regex compatible you can use "%|ZZZ" (split on % or ZZZ).

like image 94
Santiago Squarzon Avatar answered May 12 '23 09:05

Santiago Squarzon