我有一句话:
string x = "This is a first string, this is a second string.";
Run Code Online (Sandbox Code Playgroud)
将每个单词添加到数组中时
string[] words = x.Trim().Split(new char[] { ' ' });
Run Code Online (Sandbox Code Playgroud)
我只需要在数组中添加唯一的单词?
使用Linq.
此外,由于Split采用params数组,因此您不需要新的char []部分.
string[] words = x.Trim().Split(' ').Distinct().ToArray();
Run Code Online (Sandbox Code Playgroud)