Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count number of strings within a string?

How would you go about counting the number of strings within a string using Powershell?

For example:

$a = "blah test <= goes here / blah test <= goes here / blah blah"

I want to count how many times <= goes here / appears in the above.

like image 734
JBurace Avatar asked Mar 15 '13 19:03

JBurace


1 Answers

I had a string with a bunch of pipes in it. I wanted to know how many there were, so I used this to get it. Just another way :)

$ExampleVar = "one|two|three|four|fivefive|six|seven";
$Occurrences = $ExampleVar.Split("|").GetUpperBound(0);
Write-Output "I've found $Occurrences pipe(s) in your string, sir!";
  • Marcus
like image 194
BeastianSTi Avatar answered Oct 02 '22 17:10

BeastianSTi