我想从 C# 中的登录 ID 中删除域/计算机信息。所以,我想让“Domain\me”或“Domain\me”都只是“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);