谁能建议如何在字符串中找到换行符的数量。在下面的示例代码中,我有两条断线。那么如何打印显示数字为 2
String Str = "Hello <br /> How are You <br />"
谢谢!
谁能建议如何在字符串中找到换行符的数量。在下面的示例代码中,我有两条断线。那么如何打印显示数字为 2
String Str = "Hello <br /> How are You <br />"
谢谢!
这是您需要的:
String Str = @"Hello <br/> How do you feel about strange but valid <br
/>
tags?";
var regex = new Regex(@"<br\s*/>");
System.Console.WriteLine("Line breaks: {0}", regex.Matches(Str).Count);
希望这可以帮助。
//split the string
string[] splitted = Str.Replace("<br/>", "\n").Split('\n')
//output
int Count = splitted.Length
var count = Str.Split(new[] { "<br />" }, StringSplitOptions.None).Length - 1;
int pos = -1;
int count =0;
do
{
pos = Str.IndexOf(@"<br />", ++pos);
if (pos != -1)
count++;
} while (pos != -1);
Console.WriteLine(count); //2