获取替换数量 [英] Get number of replacements
本文介绍了获取替换数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有一种方法可以使用 .Replace("a","A");
吗?
Is there a method to get the number of replacements using .Replace("a", "A");
?
Example:
String string_1 = "a a a";
String string_2 = string_1.Replace("a", "A");
在这种情况下,输出应该为3,因为 a
被替换为 A
3
次.
In this case, the output should be 3, because a
was replaced with A
3
times.
推荐答案
You can get the count using .Split
function :
string_1.Split(new string[] { "a" }, StringSplitOptions.None).Length-1;
拆分字符串后,我们将再获得一项.因为 .Split
函数返回一个字符串数组,其中包含此字符串中的子字符串,这些子字符串由指定字符串数组的元素分隔.因此, Length
属性的值将为 n + 1 .
After splitting the string we will get one item more. Because, .Split
function returns a string array that contains the substrings in this string that are delimited by elements of a specified string array. So, the Length
property's value will be n+1.
这篇关于获取替换数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文