开发者

how do i delete one part of a string?

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

suggest i want to get the value of "importante" now i know how to do it with a substring, but it has 2 subistring, then how do i get, first one, and after the next? ...?? if it is开发者_StackOverflow社区 not posible i want to try it... save the first. and delete since "start" until value5 for next query save the value2... how to do any of two things?

i get the first value so...

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

    }
}


If you want to extract all values you could use a regex:

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


You can save the values in an array, instead of showing them with 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);
        }


You can use 2 methods:

String.Remove()

and

String.Replace()


use regular expression, find all the match and reconstruct the string yourself.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜