[英]Get string between a known string and any non-alphanumeric character
I have this code which allows me to get the string between "Global." 我有这段代码,可以让我获取“ Global”之间的字符串。 and " ". 和“”。
private string getGlobalVariableName(string text)
{
int pFrom = text.IndexOf("Global.") + "Global.".Length;
int pTo = text.LastIndexOf(" ");
string name = text.Substring(pFrom, pTo - pFrom);
return name;
}
I want to modify it so that it gets the string between "Global." 我想对其进行修改,以使其获得“全局”之间的字符串。 and any non-alphanumeric character. 以及任何非字母数字字符。 How could I do this? 我该怎么办?
Example: 例:
this is true for what I have now 这对于我现在拥有的是正确的
getGlobalVariableName(" foo Global.bar1 foobar") == "bar1" getGlobalVariableName(“ foo Global.bar1 foobar”)==“ bar1”
this is what I want to be able to do 这就是我想要做的
getGlobalVariableName(" foo Global.bar1>foobar") == "bar1" getGlobalVariableName(“ foo Global.bar1> foobar”)==“ bar1”
You can use Regex... 您可以使用正则表达式...
string input = "Global.bar1>foobar";
var output = Regex.Match(input, @"Global.([\w]+)").Groups[1].Value;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.