[已解决问题] 求一个简单的分析字符串的方法
提问时间: 2008-07-01 19:04
悬赏分:20 浏览:189 次

string a ="asdf>a<sdf>SDF>ttttt<sdf<<<<<dsf>>"

有这样的一个字符串,我想获取ttttt请问怎样获取?请提供方法

也就是说第三个 > 和第二个 < 之间的字符串,帮我写段小程序,非常感谢

 

提问者:潇客 - 初学一级

问题补充:规则就是>符号和<符号:
想获取第三个 > 和第二个 < 之间的字符串
最好不要用正则表达式,用字符串分析的方法实现,哪位大侠帮我解决一下,谢拉

最佳答案
static void Main(string[] args) { string a = ""; Console.WriteLine(GetWantedString(a, 3, '>', 2, '<')); Console.ReadLine(); } private static string GetWantedString(string strToSplit, int cOneIndex, char cOne, int cTwoIndex, char cTwo) { int start = 0, indexOne = 0; int end = 0, indexTwo = 0; char[] strSplited = strToSplit.ToCharArray(); for (int i = 0; i < strSplited.Length; i++) { if (strSplited[i] == cOne) { start++; if (start == cOneIndex) { indexOne = i; break; } } } for (int i = 0; i < strSplited.Length; i++) { if (strSplited[i] == cTwo) { end++; if (end == cTwoIndex) { indexTwo = i; break; } } } if (indexOne != 0 && indexTwo != 0) { return strToSplit.Substring(Math.Min(indexOne, indexTwo)+1, Math.Abs(indexTwo - indexOne)-1); } return "没有找到"; } //另外用正则表达式里的”零宽断言“也能实现
2008/7/2 0:31:11 回答者:MiniThi@k


提问者对于答案的评价:高手,佩服,非常完美,谢了,如果方便的话,能不能用零宽断言帮我写一个,正则我了解的不多,应该代码会更简洁一些吧。
其它回答(2)
这个字符串是固定的,还是有规律的?如果就你说的那个字符串,用正则匹配一下,就出来了。
2个月前   回答者:李.net - 小虾三级

2个月前   回答者:林间曦阳 - 小虾三级
评论
   您需要登录以后才能回答!
 

我要提问

我的问题


快到期问题

> 问题排行榜

相关链接