从字符串的第二行开始 - 从 - 到

Dhi*_*Dhi 3 c# string

我需要从第二行string开始'\r'直到下一个'\r'字符.

这是我的字符串中的一个考试

string str = "@b\r210.190\r\000.000\r\n";
Run Code Online (Sandbox Code Playgroud)

我需要取值210.190但内部没有 '\r'字符.

Dmi*_*nko 5

尝试使用Split:

  string str = "@b\r210.190\r\000.000\r\n";

  string result = str
    .Split(new char[] { '\r' }, 3)  // split on 3 items at most
    .Skip(1)                        // skip the 1st item 
    .FirstOrDefault();              // take the second item if exists (null if not)
Run Code Online (Sandbox Code Playgroud)

编辑:如果任意 string s(可能是null或包含10亿个字符)我建议IndexOfSubstring(因为Split创建一个可能不需要数组):

  int from = str == null ? -1 : str.IndexOf('\r');
  int length = from < 0 ? -1 : str.IndexOf('\r', from + 1) - from;

  string result = length >= 0 ? str.Substring(from + 1, length) : null;
Run Code Online (Sandbox Code Playgroud)