How do I extract a substring from a string until the second space is encountered?

后端 未结 13 1482
温柔的废话
温柔的废话 2021-02-03 23:17

I have a string like this:

\"o1 1232.5467 1232.5467 1232.5467 1232.5467 1232.5467 1232.5467\"

How do I extract only \"o1 1232.5467\"?

13条回答
  •  逝去的感伤
    2021-02-03 23:58

    Get the position of the first space:

    int space1 = theString.IndexOf(' ');
    

    The the position of the next space after that:

    int space2 = theString.IndexOf(' ', space1 + 1);
    

    Get the part of the string up to the second space:

    string firstPart = theString.Substring(0, space2);
    

    The above code put togehter into a one-liner:

    string firstPart = theString.Substring(0, theString.IndexOf(' ', theString.IndexOf(' ') + 1));
    

提交回复
热议问题