我有一句话:
string x = "This is a first string, this is a second string.";
将每个单词添加到数组中时
string[] words = x.Trim().Split(new char[] { ' ' });
我需要做什么才能将唯一的单词添加到数组中?
最佳答案
使用 Linq。
此外,由于 Split 采用参数数组,因此您不需要新的 char[] 部分。
string[] words = x.Trim().Split(' ').Distinct().ToArray();
关于c# - 当元素不存在时将元素添加到数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5870221/