string 计算字符串中的字符串数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15440333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Count number of strings within a string?
提问by JBurace
How would you go about counting the number of strings within a string using Powershell?
您将如何使用 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.
我要数一下<= goes here /
上面出现了多少次。
回答by CB.
another way (similar to @mjolinor way) in one line:
另一种方式(类似于@mjolinor 方式)在一行中:
([regex]::Matches($a, "<= goes here /" )).count
回答by mjolinor
Using regex:
使用正则表达式:
$a = "blah test <= goes here / blah test <= goes here / blah blah"
[regex]$regex = '<= goes here /'
$regex.matches($a).count
2
回答by BeastianSTi
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
- 马库斯
回答by zdan
You can use the [.NET String.Split][1]
method overload that takes an array of string objects and then count how many splits you get.
您可以使用[.NET String.Split][1]
采用字符串对象数组的方法重载,然后计算获得的拆分次数。
($a.Split([string[]]@('<= goes here /'),[StringSplitOptions]"None")).Count - 1
Note that you have to cast the string your searching for to a string array to make sure you get the correct Split
overload and then subtract 1 from the result because split will return all the strings that surround your search string. Also important is the "None" option that will cause Split to return null strings in the array (that you can count) if your search string returns at the start or end.
请注意,您必须将搜索的字符串转换为字符串数组,以确保获得正确的Split
重载,然后从结果中减去 1,因为 split 将返回搜索字符串周围的所有字符串。同样重要的是“无”选项,如果您的搜索字符串在开头或结尾返回,它将导致 Split 返回数组中的空字符串(您可以计算)。
回答by Charlie K
Just to expand on BeastianSTI' excellent answer:
只是为了扩展 BeastianSTI 的优秀答案:
Finding the maximum number of separators used in a line of a file (line unknown at run time):
查找文件一行(运行时未知的行)中使用的最大分隔符数:
$myNewCount = 0
foreach ($line in [System.IO.File]::ReadLines("Filename")){
$fields = $line.Split("*").GetUpperBound(0);
If ($fields -gt $myNewCount)
{$myNewCount = $fields}
}
回答by Stoinov
Yet another alternative one liner: (Select-String "_" -InputObject $a -AllMatches).Matches.Count
另一个替代的班轮: (Select-String "_" -InputObject $a -AllMatches).Matches.Count
回答by YenForYang
I'm surprised no one mentioned the -split
operator.
我很惊讶没有人提到-split
运营商。
For a case-sensitive match, opt for the -cSplit
operator as -split
/-iSplit
are both case-insensitive.
对于区分大小写的匹配,请选择-cSplit
运算符,因为-split
/-iSplit
都不区分大小写。
PS Y:\Power> $a = "blah test <= goes here / blah test <= goes here / blah blah"
# $a -cSplit <Delimiter>[,<Max-substrings>[,"<Options>"]]
# Default is RegexMatch (makes no difference here):
PS Y:\Power> ($a -cSplit '<= goes here /').Count - 1
2
# Using 'SimpleMatch' (the 0 means return no limit or return all)
PS Y:\Power> ($a -cSplit '<= goes here/',0,'simplematch').Count - 1
2