2011-03-31 70 views
0
String mystring="start i dont know hot text can it to have here important=value5; x=1; important=value2; z=3;"; 

建議我想要得到「importante」的值現在我知道如何使用子字符串,但它有2個子字符串,那麼我如何得到第一個,然後在下一個? ...? 如果它不是可能的我想試試它...保存第一個。並從「開始」刪除,直到value5爲下一個查詢保存值2 ... 如何做任何兩件事?如何刪除字符串的一部分?

我得到的第一個值,以...

string word = "important="; 
int c= mystring.IndexOf(word); 
int c2 = word.Length; 

for (int i = c+c2; i < mystring.Length; i++) 
{ 
    if (mystring[i].ToString() == ";") 
    { 
     break; 
    } 
    else 
    { 
     label1.Text += mystring[i].ToString(); // c# 
     // label1.setText(label1.getText()+mystring[i].ToString(); //java 

    } 
} 
+1

這是相當難以閱讀;你能否澄清一下,或許可以清楚地說明示例輸入和輸出?特別是,「importante」是不是在給定的字符串... – 2011-03-31 06:07:04

回答

1

您可以使用兩種方法:

String.Remove()

String.Replace()

+0

謝謝你的男人,我已經解決了我的問題 – angel 2011-03-31 06:24:17

+0

很高興我可以幫助 – 2011-03-31 06:25:56

0

使用正則表達式,發現所有的比賽和自己重建的字符串。

6

如果要提取所有的值,你可以使用正則表達式:

string input = "start i dont know hot text can it to have here important=value5; x=1; important=value2; z=3;"; 
Regex regex = new Regex(@"important=(?<value>\w+)"); 

List<string> values = new List<string>(); 
MatchCollection matches = regex.Matches(input); 
foreach (Match match in matches) 
{ 
    string value= match.Groups["value"].Value; 
    values.Add(value); 
} 
1

你可以將值保存在數組中,inste用MessageBox顯示它們的廣告。

 string mystring = "start i dont know hot text can it to have here important=value5; x=1; important=value2; z=3;"; 
     string temp = mystring; 
     string word = "important="; 

     while (temp.IndexOf(word) > 0) 
     { 
      MessageBox.Show(temp.Substring(temp.IndexOf(word) + word.Length).Split(';')[0]); 
      temp = temp.Remove(temp.IndexOf(word), word.Length); 
     }