大家好我想分裂的文本,并得到价值C#从字符串中获取用户名。拆分
如何我可以从这里得到Example
:
L 02/28/2012 - 04:52:05: "Example<2><VALVE_ID_PENDING><>" entered the game
我试过一大堆东西,但它是非常困难的我吗? 任何人都可以帮助我吗?
大家好我想分裂的文本,并得到价值C#从字符串中获取用户名。拆分
如何我可以从这里得到Example
:
L 02/28/2012 - 04:52:05: "Example<2><VALVE_ID_PENDING><>" entered the game
我试过一大堆东西,但它是非常困难的我吗? 任何人都可以帮助我吗?
可以尝试像
string s = "L 02/28/2012 - 04:52:05: \"Example<2><VALVE_ID_PENDING><>\" entered the game";
int start = s.IndexOf('"')+1;
int end = s.IndexOf('<');
var un = s.Substring(start, end-start);
// say you have your text in the text variable
var yourExtractedText = text.Split('"').Split('<')[0];
小心,这会导致异常,如果串变化的格式。
您需要的输出字符串开始前给角色你做myString.Split('"');
你会得到字符串数组是
string[] myStringArray = myString.Split('"');
myStringArray[0] contains L 02/28/2012 - 04:52:05:
myStringArray[1] contains Example<2><VALVE_ID_PENDING><>"
myStringArray[2] contains entered the game
你可以把这个逻辑并建立所需的字符串。
记得在再次构造字符串时使用StringBuilder。
你可能会考虑使用正则表达式来找到你在找什么:
Match match = Regex(inputString, "\"([\w\s]+)<");
if (match.Success) {
String username = match.Groups[1].Value;
}
小心包括你知道的信息必须存在。例如,如果你知道您的用户名以引号开始,仅由字字符和空格,并用尖括号分隔的数字结束,你可以写:
Match match = Regex(inputString, "\"([\w\s]+)<[0-9]+>");
if (match.Success) {
String username = match.Groups[1].Value;
}
给你:
private void button2_Click(object sender, EventArgs e)
{
string temp = GetExample("L 02/28/2012 - 04:52:05: \"Example<2><VALVE_ID_PENDING><>\" entered the game");
}
private string GetExample(string text)
{
int startIndex = text.IndexOf('"');
int endIndex = text.IndexOf('<');
return text.Substring(startIndex + 1, endIndex - startIndex - 1);
}
不要忘记,"
之前在一个字符串应该来一个\
。
向我们展示您的尝试。 – 2012-02-28 23:59:16