做模拟登录时,需要截取html代码中的名字,返回的字符串内容如下
<span class="welcome">您好<span style="font-size:20px;weight:bold">
<span id="LabUserName">王霞</span>
</span>,欢迎您!</span>
我后台要怎样截取得到王霞
有人说用正则,
正则用起来不顺手,后用分割得到名字,作法,
//<span id="LabUserName">王霞</span> string[] sArray = srcString.Split(new string[] { "<span id="LabUserName"> ", "</span>" }, StringSplitOptions.RemoveEmptyEntries); string needname = sArray[0]; int prenamelength = needname.LastIndexOf("<span id="LabUserName">"); int pl = "<span id="LabUserName">".Length; string ssonanme = ""; if (prenamelength >= 0) { ssonanme = needname.Substring(prenamelength+pl); Session["username"] = ssonanme; } else { Session["username"] = username; }
这样就能模拟得到王霞了。
另外正则参考:http://www.cnblogs.com/Fooo/archive/2010/10/22/1858121.html