我想削减sring的结尾:
输入字符串:
/O=Shore Tel/OU=CANDY/cn=Recipients/cn=PAgricola
/O=Shore Tel/OU=CANDY/cn=Recipients/cn=YAchmanov
/o=Shore Tel/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=Mukul Agrawal2f2
/o=Shore Tel/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=Nick Aiello816
输出字符串:
/O=Shore Tel/OU=CANDY/cn=Recipients
/O=Shore Tel/OU=CANDY/cn=Recipients
/o=Shore Tel/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients
/o=Shore Tel/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients
你能给我一个建议吗?
答案 0 :(得分:0)
看起来您可以找到"/cn="
的最后一个索引,以及从零到该位置的子串:
int pos = str.LastIndexOf("/cn=");
if (pos > 0) {
str = str.Substring(0, pos);
}
答案 1 :(得分:0)
LINQ中只有一行
string input = @"/O=Shore Tel/OU=CANDY/cn=Recipients/cn=PAgricola
/O=Shore Tel/OU=CANDY/cn=Recipients/cn=YAchmanov
/o=Shore Tel/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=Mukul Agrawal2f2
/o=Shore Tel/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=Nick Aiello816";
var result = string.Join("\n", input.Split('\n')
.Select(x => x.Substring(0, x.LastIndexOf('/')))
.ToArray());
Console.WriteLine(result);
为了便于阅读,我使用了verbatim @字符并对齐了子字符串,但这会引入一些不需要的空格。目前尚不清楚输入字符串是如何格式化的,但这里的关键点是设置结果变量的行。
答案 2 :(得分:0)
string s = "/O=Shore Tel/OU=CANDY/cn=Recipients/cn=PAgricola";
s=s.Substring(0, s.IndexOf("s/cn="));