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 '

你可能感兴趣的:(C#)