我想拆分给定的字符串并从该字符串中删除重复项.就像我有以下字符串:
这是我在堆栈溢出中的第一篇文章,我在开发方面非常新,我对如何发布问题没有更多的了解.
现在我想用白色空格拆分整个字符串,新数组将没有重复的条目.
我怎样才能做到这一点?
Ada*_*lph 12
"This is my first post in stack overflow, I am very new in development and I did not have much more idea about the how to post the question."
.Split() // splits using all white space characters as delimiters
.Where(x => x != string.Empty) // removes an empty string if present (caused by multiple spaces next to each other)
.Distinct() // removes duplicates
Run Code Online (Sandbox Code Playgroud)
Distinct()并且Where()是LINQ扩展方法,因此您必须拥有using System.Linq;源文件.
上面的代码将返回一个实例IEnumerable<string>.您应该能够使用此操作执行大多数操作.如果您确实需要数组,则可以附加.ToArray()到语句中.