c# 在另一个字符串中获取一个字符串

标签 c#

我有这样一个字符串:

 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/

相关文章:

c# - 使用 Linq 查询服务引用

c# - 定时器使用标签

c# - 即使使用 ConfigureAwait(false),在 WebAPI 中同步调用异步方法也会死锁

c# - 在 Xamarin 表单中嵌入单个 Youtube 视频,但不是 Youtube 列表

c# - 序列化和反序列化多个对象

c# - Exchange Web 服务托管 API : How can I perform Recurrence Expansion through the FindItem Method?

c# - 如何解决 Unity3D iOS 构建中的 AOT 和代码剥离

c# - 使用 C# 在通用列表中查找最大整数?

c# - 代码检查说我需要处理对象。哪一个?

c# - 发送 http header 后服务器无法设置状态