C# 字符串提取帧头帧尾之间的字符串

描述

在下位机通讯中,经常使用带帧头帧尾的协议。此时需要将采集上来的数据进行识别处理。

代码示例

string factMessage = "Extension methods have all the capabilities of regular static methods.";

// Write the string and include the quotation marks.
Console.WriteLine($"\"{factMessage}\"");

// This search returns the substring between two strings, so 
// the first index is moved to the character just after the first string.
int first = factMessage.IndexOf("methods") + "methods".Length;
int last = factMessage.LastIndexOf("methods");
string str2 = factMessage.Substring(first, last - first);
Console.WriteLine($"Substring between \"methods\" and \"methods\": '{str2}'");

运行结果

"Extension methods have all the capabilities of regular static methods."
Substring between "methods" and "methods": ' have all the capabilities of regular static '
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页