C#关于字符串处理求助
请问如何用最简洁的方法将<NickName>Hik-Naming</NickName>中“-”之前的字符提取出来呢,即:Hik
------解决方案--------------------本帖最后由 caozhy 于 2013-11-08 11:58:51 编辑
string s = "<NickName>Hik-Naming</NickName>";
string name = Regex.Match(s, @"(?<=\<NickName\>)[^\<]+(?=\<\/NickName\>)").Value;
string firstname = name.Split('-')[0];
------解决方案-------------------- string str = "<NickName>Hik-Naming</NickName>";
var sp=str.Split('-');
var rest= sp[0].Substring(sp[0].IndexOf('>')+1);
------解决方案-------------------- var s1 = str.Substring(str.IndexOf('>') + 1);
var s2 = s1.Substring(0,s1.IndexOf('<'));
string firstname = s2.Split('-')[0];
------解决方案--------------------如果是xml的话。SelectSingleNode("NickName").innerText可得到Hik-Naming
或者直接用正则
string s = "<NickName>Hik-Naming</NickName>";
string name = Regex.Match(s, @"(?i)(?<=(<NickName>))[^-]+(?=[^<]+</NickName>)").Value;