我需要从 string
中提取 second line,它从 '\r'
开始直到下一个 '\r'
字符。
这是我的字符串中的一个例子
string str = "@b\r210.190\r\000.000\r\n";
我需要取值 210.190
但没有 '\r'
字符在里面。
最佳答案
尝试使用拆分
:
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)
编辑:如果是任意 string
(可以是null
或包含 10 亿 个字符)我建议使用IndexOf
和Substring
(因为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;
关于c# - 从字符串开始的第二行开始 - 直到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52307484/