本文介绍了将字符串分割与斜线的空间和斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我wan't由斜线分割字符串 我当前的代码如下所示: 字符串值=Ctws CWTS / ROTC /廖运华Ctws 的String []标记= value.Split(新的char [] {'/'},StringSplitOptions.RemoveEmptyEntries); 的foreach(在令牌VAR令牌) { Console.Write(标记); } 的输出是这一个:CWTS CWTS,ROTC ,廖运华CWTS 现在我希望我的输出中是这样的:CWTS,后备军官训练队,廖运华 有些答案表明,我会用鲜明的() > 如果什么值:Something1 CWTS / ROTC /廖运华Something2 的输出应该是相同的:CWTS,后备军官训练队,廖运华 解决方案 刚使用鲜明 的String []标记=值 .Split(新的char [] {'/',''} ,StringSplitOptions.RemoveEmptyEntries) .Distinct(); 另外,不要忘了包括的空格应用于seperators 根据您的编辑,你可以做下面的,如果你想exacly这些值:CWTS,后备军官训练队,廖运华所有输入 VAR值=新的List<串GT; {CWTS,后备军官训练队,廖运华}; 的String []标记=值 .Split(新的char [] {'/',''},StringSplitOptions.RemoveEmptyEntries)。凡(T => values.Contains(T )) .Distinct(); I wan't to split the string by forward slashMy current code looks like this: string value = "Ctws Cwts/Rotc/Lts Ctws"; string[] tokens = value.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries); foreach (var token in tokens) { Console.Write(token); }The output is this one: "Cwts Cwts", "Rotc", "Lts Cwts"Now I want my ouput to be this one: "Cwts", "Rotc", "Lts"Edited:Some of the answers suggest that I will used Distinct()What if the value is: "Something1 Cwts/Rotc/Lts Something2"The output should be the same: "Cwts", "Rotc", "Lts" 解决方案 Just use Distinct string[] tokens = value .Split(new char[] { '/', ' ' }, StringSplitOptions.RemoveEmptyEntries) .Distinct();Also don't forget to include space to seperatorsAccording to your edit you can do the following if you want exacly these values: "Cwts", "Rotc", "Lts" for all inputsvar values = new List<string> { "Cwts", "Rotc", "Lts" };string[] tokens = value .Split(new char[] { '/', ' ' }, StringSplitOptions.RemoveEmptyEntries) .Where(t => values.Contains(t)) .Distinct(); 这篇关于将字符串分割与斜线的空间和斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-18 21:48