我有这样一个字符串:
some_string = "A simple demo of SMS text messaging.\r\n+CMGW: 3216\r\n\r\nOK\r\n\"
我来自 vb.net,我需要在 C# 中知道,如果我知道 CMGW 的位置,我如何从那里得到“3216”?
我知道我的开始应该是CMGW + 6的位置,但是我如何让它一找到“\r”就停止??
同样,我的最终结果应该是 3216
谢谢!
最佳答案
从您感兴趣的位置开始查找\r
的索引,并使用Substring
overload which takes a length :
// Production code: add validation here.
// (Check for each index being -1, meaning "not found")
int cmgwIndex = text.IndexOf("CMGW: ");
// Just a helper variable; makes the code below slightly prettier
int startIndex = cmgwIndex + 6;
int crIndex = text.IndexOf("\r", startIndex);
string middlePart = text.Substring(startIndex, crIndex - startIndex);
关于c# 在另一个字符串中获取一个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3050747/