计算新线的出现次数?
例如,计算新线的出现次数?
string="help/nsomething/ncrayons"
输出:
String word count is: 3
这是我有什么,但在程序循环,虽然该方法几次,它看起来像我只得到最后创建的字符串。这里的代码块:
Regex regx = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", RegexOptions.IgnoreCase);
MatchCollection matches = regx.Matches(output);
//int counte = 0;
foreach (Match match in matches)
{
//counte = counte + 1;
links = links + match.Value + '\n';
if (links != null)
{
string myString = links;
string[] words = Regex.Split(myString, @"\n");
word_count.Text = words.Length.ToString();
}
}
它是\n
换行。
不知道正则表达式是你的情况必须的,但你可以使用split
:
string myString = "help/nsomething/ncrayons";
string[] separator = new string[] { "/n" };
string[] result = myString.Split(separator, StringSplitOptions.None);
MessageBox.Show(result.Count().ToString());
使用正则表达式的另一种方式:与/ n的整个问题,你是混合\ n
string myString = "help/nsomething/ncrayons";
string[] words = Regex.Split(myString, @"/n");
word_count.Text = words.Length;
当我尝试使用“Count”时,出现以下错误:Error 'System.Array'不包含'Count'的定义,也没有扩展方法'Count'接受类型的第一个参数'System.Array'可以找到(你是否缺少使用指令或程序集引用?) – Jason 2011-04-22 21:22:12
@Jason它适用于我在VS 2010,.net 3.5和4.0上的罚款你的目标是什么框架? – Prix 2011-04-22 21:25:43
我正在使用4.0。我使用这个代码,它不喜欢它:word_count.Text = result.Count()。ToString(); – Jason 2011-04-22 21:26:47
。我们可以假设你的意思是\ n对于换行符,但如果你能清楚这一点会更好吗? – rtpHarry 2011-04-22 22:15:07