我会为此建议简单的正则表达式。看看这个例子:using System.Diagnostics;using System.Text.RegularExpressions;...Regex regex = new Regex("<A>(.*?)<B><BEOF>(.*?)<AEOF>");string myString = @"<START><A>message<B><BEOF>UnknownLengthOfText<AEOF><A>message<B><BEOF>some other line of text<AEOF><END>";MatchCollection matches = regex.Matches(myString);foreach (Match m in matches){ Debug.WriteLine(m.Groups[1].ToString(), m.Groups[2].ToString());}编辑:由于字符串在一行中,正则表达式应该是“懒惰的”,用惰性量词标记?。此外,我更改了正则表达式,以便它也使用 sTrenat 的建议来自动解析消息和标题。所以,而不是Regex regex = new Regex("<A>(.*)<AEOF>");我用了Regex regex = new Regex("<A>(.*?)<B><BEOF>(.*?)<AEOF>");注意附加的?标记惰性量词,当它找到标签之间的第一个匹配时停止(没有?整个 strign 将被捕获,而不是n标签之间的消息)
用这样的东西试试:string test = @"<START> <A>message<B><BEOF>UnknownLengthOfText<AEOF> <A>message<B><BEOF>UnknownLengthOfText<AEOF> <END>";//for this test this will give u an array containing 3 items...string[] tmp1 = test.Split("<AEOF>");//here u will store your results inList<string> results = new List<string>();//for every single one of those 3 items:foreach(string item in tmp1){ //this will only be true for the first and second item if(item.Contains("<A>")) { string[] tmp2 = item.Split("<A>"); //As the string you are looking for is always BEHIND the <A> you //store the item[1], (the item[0] would be in front) results.Add(tmp2[1]); }}