2009-07-04 250 views
0

如何获取带有自定义函数的括号内的字符串?字符串子字符串函数

e.x.字符串“GREECE(+30)”应该只返回“+30”

+2

我想说的正则表达式是能够做到这一点。 – luiscubal 2009-07-04 17:40:46

回答

5

有一些不同的方法。

纯字符串方法:

Dim left As Integer = str.IndexOf('(') 
Dim right As Integer= str.IndexOf(')') 
Dim content As String = str.Substring(left + 1, right - left - 1) 

正则表达式:

Dim content As String = Regex.Match(str, "\((.+?)\)").Groups[1].Value 
3

对于一般问题,我建议使用Regex。但是,如果你知道有关输入字符串(只有一组括号中,右括号前开括号)的格式,这会工作:

int startIndex = s.IndexOf('(') + 1; 
string result = s.Substring(startIndex, s.LastIndexOf(')') - startIndex); 
0

你可以看一个正则表达式,或者以其他方式与玩IndexOf()功能

1

随着regular expressions

Dim result as String = System.Text.RegularExpressions.Regex.Match("GREECE (+30)", "\((?<Result>[^\)]*)\)").Groups["Result"].Value; 

代码未经测试,但我希望只有编译问题。

0

在Python,使用字符串指数法和切片:

>>> s = "GREECE(+30)" 
>>> s[s.index('(')+1:s.index(')')] 
'+30'