从 C# 中的登录 ID 中删除域信息
我想从 C# 中的登录 ID 中删除域/计算机信息。 所以,我想让“Domain\me”或“Domain\me”只是“我”。 我总是可以检查其中任何一个是否存在,并使用它作为索引来启动子字符串......但我正在寻找更优雅和紧凑的东西。
最坏的情况:
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);
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
当你只有一把锤子时,一切看起来都像钉子......
使用刀片----
when all you have is a hammer, everything looks like a nail.....
use a razor blade ----
您可以滥用 Path 类,因此:
You could abuse the Path class, thusly:
怎么样:
How's about:
这适用于两者,但适用于命名组。
This will work for both but with named groups.
我总是这样做:
现在你可以查看domainuser.Length来看看有多少部分,domainuser[0]表示域,domainuser[1]表示用户名。
I always do it this way:
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.
这适用于两个有效的域登录:
This works for both valid domain logins:
小猪支持德里克·斯莫斯的回答……
对我有用。
Piggy backing on Derek Smalls Answer...
worked for me.
作为 @Drew 答案的改进
,这样您还可以匹配带域和不带域的用户名
As an improvement of @Drew's answer
This way you can also match usernames with and without domain