计算字符串中的字符串数量?

JBu*_*ace 6 string powershell

你会如何使用Powershell计算字符串中的字符串数?

例如:

$a = "blah test <= goes here / blah test <= goes here / blah blah"
Run Code Online (Sandbox Code Playgroud)

我想算一下<= goes here /上面出现的次数.

CB.*_*CB. 24

另一种方式(类似于@mjolinor方式)在一行中:

([regex]::Matches($a, "<= goes here /" )).count
Run Code Online (Sandbox Code Playgroud)


Bea*_*STi 7

我有一根绳子,里面有一堆管子。我想知道有多少,所以我用这个来得到它。只是另一种方式:)

$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!";
Run Code Online (Sandbox Code Playgroud)
  • 马库斯


mjo*_*nor 5

使用正则表达式:

$a = "blah test <= goes here / blah test <= goes here / blah blah"
[regex]$regex = '<= goes here /'
$regex.matches($a).count
2
Run Code Online (Sandbox Code Playgroud)