Extract two substrings from a string

前端 未结 1 966
心在旅途
心在旅途 2021-01-29 14:13

what is the best and optimise wayto extract substrings from a specified string.

my primary string is like

string str = \"ACK

        
相关标签:
1条回答
  • 2021-01-29 14:46
    string str = @"<ABCMSG><t>ACK</t><t>AAA0</t><t>BBBB1</t></ABCMSG>";
    var matches = Regex.Matches(str, @"<t>(\w+)<\/t>");
    
    Console.WriteLine(matches[1].Groups[1]);    // outputs "AAAA1"
    Console.WriteLine(matches[2].Groups[1]);    // outputs "BBB2"
    

    This assumes that your data are always inside a <t></t> tag, also you might want to do some error catching in case no matches are found.

    0 讨论(0)
提交回复
热议问题