Reputation: 1712
I have web method to get user from this.User.Identity.Name.string username = "ACCOUNTS\Ninja.Developer" I want to convert username part after "ACCOUNTS\" to lower case to be username = "ACCOUNTS\ninja.developer"
public User GetUser()
{
var user = new User
{
Username = this.User.Identity.Name,<-- convert it here
IsAuthenticated = this.User.Identity.IsAuthenticated
};
return user;
}
note: double \ not single \
Upvotes: 2
Views: 104
Reputation: 39453
Use this code:
var Identity = this.User.Identity.Name;
var Username = Identity.Split('\\')[0] + @"\\" + Identity.Split('\\')[2].ToLower();
Of course you should check before in the name have the \
character, etc.
Upvotes: 5
Reputation: 700
As mentioned in other answers, you can use Regex or Split, but here's a substring approach specific to your case.
var user = new User
{
Username = this.User.Identity.Name.Substring(0,9) + this.User.Identity.Name.Substring(9, name.Length - 9).ToLower(),
IsAuthenticated = this.User.Identity.IsAuthenticated
};
Upvotes: 1
Reputation: 14477
You can use Regex.Replace
to achieve it :
Username = Regex.Replace(this.User.Identity.Name, @"(?<=ACCOUNTS\\).+", n => n.Value.ToLower()),
The regex pattern (?<=ACCOUNTS\\).+
will match for anything after ACCOUNTS\
, and the match is then replaced by its lower case equivalent.
Upvotes: 3