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

后端 未结 13 1731
温柔的废话
温柔的废话 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

    I would recommend a regular expression for this since it handles cases that you might not have considered.

    var input = "o1 1232.5467 1232.5467 1232.5467 1232.5467 1232.5467 1232.5467";
    var regex = new Regex(@"^(.*? .*?) ");
    var match = regex.Match(input);
    if (match.Success)
    {
        Console.WriteLine(string.Format("'{0}'", match.Groups[1].Value));
    }
    

提交回复
热议问题