2012-02-28 43 views
0

大家好我想分裂的文本,並得到價值C#從字符串中獲取用戶名。拆分

如何我可以從這裏得到Example

L 02/28/2012 - 04:52:05: "Example<2><VALVE_ID_PENDING><>" entered the game 

我試過一大堆東西,但它是非常困難的我嗎? 任何人都可以幫助我嗎?

+3

向我們展示您的嘗試。 – 2012-02-28 23:59:16

回答

1

可以嘗試像

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); 
1
// say you have your text in the text variable 
var yourExtractedText = text.Split('"').Split('<')[0]; 

小心,這會導致異常,如果串變化的格式。

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。

0

你可能會考慮使用正則表達式來找到你在找什麼:

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; 
} 
0

給你:

 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); 
    } 

不要忘記,"之前在一個字符串應該來一個\