如果名稱有'反斜槓',你應該找到一個反斜槓,如果名稱有一個垂直標籤(轉義'\ v'),你就不會找到反斜槓。在第二個版本中找到反斜槓的原因是因爲您使用了@和字符串聲明more info。
試試這個:
string strUser = this.Context.User.Identity.Name;
strUser = strUser.Substring(strUser.LastIndexOf("\v") + 1);
EDITED
如果你知道哪些逃脫你正在尋找你也可以使用這個字符:
string strUser = this.Context.User.Identity.Name;
strUser = strUser.Substring(strUser.LastIndexOfAny(new char[]{'\b','\t','\n','\v','\f','\r','\\'}) + 1);
如果你知道有恰好一個跟在域名後面的字符,你也可以使用Regex來獲取用戶。
string strUser = this.Context.User.Identity.Name;
strUser = Regex.Replace(strUser, @"^(xxx).(.+)$", "$2");
// or
strUser = Regex.Replace(strUser, @"^([a-zA-Z]*?)[^a-zA-Z](.+)$", "$2");
// or
strUser = Regex.Replace(strUser, @"^(.*?)[\t\n\v\f\r\\/<> ,[email protected]](.+)$", "$2");
// it all depends on your input
你能否給我們提供'this.Context.User.Identity.Name'的實際值? – Nayan 2010-10-13 21:56:52
我結束了使用System.Environment.UserName而不是Identity.Name。這樣我不需要獲取子字符串。 – Jeff 2010-10-14 01:51:07