日期:2014-05-19  浏览次数:20879 次

要得到指定的字符在一个字符串中第10次出现的位置
在一个打印页面,需要控制输出行数为10行,
所以想找到输出字符串中第10次出现回车符¥r¥n的位置,
然后将其后的字符切去
请问有什么办法可以实现???

------解决方案--------------------
StringBuilder sb=new StringBuilder();
for(int i=0;i <20;i++){
sb.AppendFormat( "{0}\r\n ",i);
}
string str=sb.ToString();

Match match=Regex.Match(str,@ "([^\r]*\r\n){0,10} ");
Console.WriteLine(match.Groups[0].Value);
------解决方案--------------------
string s = "0\r\n1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n11\r\n12\r\n13 ";
MatchCollection vMatches = Regex.Matches(s, "\r\n ");
if (vMatches.Count > = 10)
MessageBox.Show(s.Substring(0, vMatches[9].Index));
--
0
1
2
3
4
5
6
7
8
9