C# 字串提取幀頭幀尾之間的字串
阿新 • • 發佈:2018-11-25
目錄
描述
在下位機通訊中,經常使用帶幀頭幀尾的協議。此時需要將採集上來的資料進行識別處理。
程式碼示例
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 '