在C#中的登录ID删除域信息 [英] Remove domain information from login id in C#

查看:125
本文介绍了在C#中的登录ID删除域信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除在C#中的登录ID域/计算机信息。所以,我想提出要么Domain\me或Domain\me只是我。我总能检查任的存在,并用其作为索引启动子...但我寻找的东西体积更小巧。



更糟情况:

  INT的startIndex = 0; 
INT indexOfSlashesSingle = ResourceLoginName.IndexOf(\);
INT indexOfSlashesDouble = ResourceLoginName.IndexOf(\\);
如果(indexOfSlashesSingle!= -1)
的startIndex = indexOfSlashesSingle;
,否则
的startIndex = indexOfSlashesDouble;
串SHORTNAME = ResourceLoginName.Substring(的startIndex,ResourceLoginName.Length-1);


解决方案

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



使用刀片----

 使用系统;使用System.Text.RegularExpressions 
;
公共MyClass类
{
公共静态无效的主要()
{
串domainUser = Regex.Replace(domain\\user,*。 \\\\,$ 1,RegexOptions.None)(*);
Console.WriteLine(domainUser);

}

}


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.

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

解决方案

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",".*\\\\(.*)", "$1",RegexOptions.None);
    	Console.WriteLine(domainUser);	

    }

}

这篇关于在C#中的登录ID删除域信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆