从 C# 中的登录 ID 中删除域信息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/185559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:04:23  来源:igfitidea点击:

Remove domain information from login id in C#

c#stringindexof

提问by Dan

I would like to remove the domain/computer information from a login id in C#. So, I would like to make either "Domain\me" or "Domain\me" just "me". I could always check for the existence of either, and use that as the index to start the substring...but I am looking for something more elegant and compact.

我想从 C# 中的登录 ID 中删除域/计算机信息。所以,我想让“域\我”或“域\我”只是“我”。我总是可以检查两者是否存在,并将其用作索引来启动子字符串......但我正在寻找更优雅和紧凑的东西。

Worse case scenario:

更坏的情况:

int startIndex = 0;
int indexOfSlashesSingle = ResourceLoginName.IndexOf("\");
int indexOfSlashesDouble = ResourceLoginName.IndexOf("\");
if (indexOfSlashesSingle != -1)
    startIndex = indexOfSlashesSingle;
else
    startIndex = indexOfSlashesDouble;
string shortName = ResourceLoginName.Substring(startIndex, ResourceLoginName.Length-1);

采纳答案by user26350

when all you have is a hammer, everything looks like a nail.....

当你只有一把锤子时,一切看起来都像钉子......

use a razor blade ----

使用剃须刀片----

using System;
using System.Text.RegularExpressions;
public class MyClass
{
    public static void Main()
    {
        string domainUser = Regex.Replace("domain\user",".*\\(.*)", "",RegexOptions.None);
        Console.WriteLine(domainUser);  

    }

}

回答by BoltBait

I always do it this way:

我总是这样做:

    string[] domainuser;
    string Auth_User = Request.ServerVariables["AUTH_USER"].ToString().ToLower(); 
    domainuser = Auth_User.Split('\');

Now you can look at domainuser.Length to see how many parts are there and domainuser[0] for the domain and domainuser[1] for the username.

现在,您可以查看 domainuser.Length 以查看其中有多少部分, domainuser[0] 表示域, domainuser[1] 表示用户名。

回答by Matt Dawdy

        string theString = "domain\me";
        theString = theString.Split(new char[] { '\' })[theString.Split(new char[] { '\' }).Length - 1];

回答by Jeffrey L Whitledge

You could abuse the Path class, thusly:

您可以滥用 Path 类,因此:

string shortName = System.IO.Path.GetFileNameWithoutExtension(ResourceLoginName);

回答by Drew Graham

How's about:

怎么样:

string shortName = ResourceLoginName.Split('\')[1]

回答by anyhotcountry

This works for both valid domain logins:

这适用于两个有效的域登录:

var regex = @"^(.*\)?([^\@]*)(@.*)?$";
var user = Regex.Replace("domain\user", regex, "", RegexOptions.None);
user = Regex.Replace("[email protected]", regex, "", RegexOptions.None);

回答by Derek Smalls

This will work for both but with named groups.

这将适用于两者,但适用于命名组。

^(?<domain>.*)\(?<username>.*)|(?<username>[^\@]*)@(?<domain>.*)?$

回答by Earl

Piggy backing on Derek Smalls Answer...

小猪支持德里克·斯莫尔斯的回答......

Regex.Replace(User.Identity.Name,@"^(?<domain>.*)\(?<username>.*)|(?<username>[^\@]*)@(?<domain>.*)?$", "${username}", RegexOptions.None)

worked for me.

为我工作。