关于C#中子字符串的简单问题

ado*_*lot 0 c# string substring

这可能太容易了,但我不知道从哪里开始.

我有像"'02HEX'aspoodsasas'CR''LF'这样的叮当声"

现在我想在char(02)和char(12)之间提取stings;

到现在为止我做了以下

string s = string.Format("{0}{1}{2}", (char)02, "12345678", (char)12);
int chindx = s.IndexOf((char)02)+1;
s = s.Substring(chindx, 8)
Run Code Online (Sandbox Code Playgroud)

我的问题是如果我知道我的字符串中的起始字符的位置和结束字符的位置,如何确定我的子字符串的长度

Jon*_*eet 8

只需减去:

string middle = text.Substring(start, end - start);
Run Code Online (Sandbox Code Playgroud)

(假设您希望角色处于位置end- 如果您这样做,只需添加一个.)

例如:

string text = "hi there";
int start = 3; // 't'
int end = 6; // 'r'
string middle = text.Substring(start, end - start); // "the"
Run Code Online (Sandbox Code Playgroud)