积极答复者
如何截取字符串

问题
-
360安全网址 (http://hao.360.cn/?360safe)
上面一行举例子的字符串,我想类似于这种格式的字符串都截取括号中的网址,每个这种字符串都有括号,但字符串的长短不一样
答案
-
1.如果想把括号去掉,代码如下:
private void button1_Click(object sender, EventArgs e) { string str = "360安全网址 (http://hao.360.cn/?360safe) 百度 (http://www.baidu.com/) csdn (http://blog.csdn.net/zx13525079024)"; string pattern =@"\(([^\)]*)\)"; MatchCollection mc = Regex.Matches(str,pattern); foreach (Match item in mc) { string wz = item.Value; wz = wz.Substring(1, wz.Length -2); MessageBox.Show(wz); } }
2. 请学习下正则表达式
http://deerchao.net/tutorials/regex/regex.htm
http://blog.csdn.net/zx13525079024
- 已标记为答案 求知与释疑 2012年8月13日 11:01
全部回复
-
private void button1_Click(object sender, EventArgs e) { string str = "360安全网址 (http://hao.360.cn/?360safe) 百度 (http://www.baidu.com/) csdn (http://blog.csdn.net/zx13525079024)"; string pattern =@"\(([^\)]*)\)"; MatchCollection mc = Regex.Matches(str,pattern); foreach (Match item in mc) { string wz = item.Value; MessageBox.Show(wz); } }
http://blog.csdn.net/zx13525079024
-
1.如果想把括号去掉,代码如下:
private void button1_Click(object sender, EventArgs e) { string str = "360安全网址 (http://hao.360.cn/?360safe) 百度 (http://www.baidu.com/) csdn (http://blog.csdn.net/zx13525079024)"; string pattern =@"\(([^\)]*)\)"; MatchCollection mc = Regex.Matches(str,pattern); foreach (Match item in mc) { string wz = item.Value; wz = wz.Substring(1, wz.Length -2); MessageBox.Show(wz); } }
2. 请学习下正则表达式
http://deerchao.net/tutorials/regex/regex.htm
http://blog.csdn.net/zx13525079024
- 已标记为答案 求知与释疑 2012年8月13日 11:01