How can I ignore escape character Vertical tab
I have a issue when I look up a user name using the code below if the user has \v in their name. For Example, if their user name is xxx\vtest, it can't find the backslash when using the LastIndexOf fun开发者_如何学JAVAction.
string strUser = this.Context.User.Identity.Name;
strUser = strUser.Substring(strUser.LastIndexOf("\\") + 1);
If I do try this, it works with no problems.
string strUser = @"xxx\vtest";
strUser = strUser.Substring(strUser.LastIndexOf("\\") + 1);
Any ideas on how to ignore the escape character while using User.Identity.Name?
If the name has 'backslash v' you should find a backslash, if the name has a vertical tab (escaped '\v') you will not find a backslash. The reason you find a backslash in the second version is because you used an @ with the string declaration more info.
try this:
string strUser = this.Context.User.Identity.Name;
strUser = strUser.Substring(strUser.LastIndexOf("\v") + 1);
EDITED
If you know which escape characters you are looking for you can also use this:
string strUser = this.Context.User.Identity.Name;
strUser = strUser.Substring(strUser.LastIndexOfAny(new char[]{'\b','\t','\n','\v','\f','\r','\\'}) + 1);
If you know that there is exactly one character that follows the domain you can also use Regex to get the user.
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\\/<> ,.@](.+)$", "$2");
// it all depends on your input
精彩评论