将 "ACCOUNTS\" 之后的隐藏名称部分转换为小写?

Covert name part after "ACCOUNTS\\" to lower case?

我有从 this.User.Identity.Name.string username = "ACCOUNTS\Ninja.Developer" 获取用户的网络方法 我想将 "ACCOUNTS\" 之后的用户名部分转换为小写,成为 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;
}

注意:双\不是单\

使用此代码:

var Identity = this.User.Identity.Name;
var Username = Identity.Split('\')[0] + @"\" + Identity.Split('\')[2].ToLower();

当然你应该检查之前在名称中有\字符等

你可以用Regex.Replace来实现:

Username = Regex.Replace(this.User.Identity.Name, @"(?<=ACCOUNTS\).+", n => n.Value.ToLower()),

正则表达式模式 (?<=ACCOUNTS\).+ 将匹配 ACCOUNTS\ 之后的任何内容,然后将匹配项替换为对应的小写字母。

如其他答案中所述,您可以使用 Regex 或 Split,但这里有一种特定于您的情况的子字符串方法。

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
};