-11

我有两个单独的字符串:

string s1 = "Hello welcome to the world of C sharp";

String s2 = "Hello world welcome to the world of C";

现在我想获取两个字符串中的唯一单词,例如{sharp}.

我也想在同一个程序中找到相似的词,比如{Hello, welcome, to, the , world of, C}.

我无法继续。任何人都可以帮忙吗?

4

6 回答 6

4

在 C# 中,您可以使用:

string[] words1 = s1.Split(" ", StringSplitOptions.RemoveEmptyEntries);
string[] words2 = s2.Split(" ", StringSplitOptions.RemoveEmptyEntries);

// Retrieve words that only exist in one list
var unique = words1.Except(words2).Concat(words2.Except(words1)); 

// Retrieve all "similar words" - exist in either list
var matches = words1.Intersect(words2);
于 2012-09-17T18:19:43.840 回答
1

我建议使用Split()and Except()

        string s1 = "Hello welcome to the world of C sharp";

        string s2 = "Hello world welcome to the world of C";

        var s1Words = s1.Split(' ', StringSplitOptions.RemoveEmptyEntries);
        var s2Words = s2.Split(' ', StringSplitOptions.RemoveEmptyEntries);

        var s1Only = s1Words.Except(s2Words);
        var s2Only = s2Words.Except(s1Words);

        Console.WriteLine("The unique words in S1 are: " + string.Join(",", s1Only));
        Console.WriteLine("The unique words in S2 are: " + string.Join(",", s2Only));

如果您需要它们在同一个列表中,您可以使用Concat()

var allUniqueWords = s1Only.Concat(s2Only);

您还可以使用以下方法找到相同的单词Intersect()

var sameWords = s1Words.Intersect(s2Words);

LINQ 中的集合操作非常适合这类事情。还有一个Union()可以为您提供两者中所有单词的不同列表,例如:

var allWords = s1Words.Union(s2Words);
于 2012-09-17T18:19:22.053 回答
0

老实说,我不确定您要做什么,但这里有一些可能的答案:

获取仅存在于一个字符串或另一个字符串中的单词:

using System.Linq;
...
string s1 ="Hello welcome to the world of C sharp";
string s2 = "Hello world welcome to the world of C"; 
List<string> s1List = (s1 + " " + s2)
            .Split(' ')
            .Where(s=> (!s2.Split(' ').Contains(s) || !s1.Split(' ').Contains(s)))
            .Distinct()
            .ToList(); 

获取所有独特的单词:

using System.Linq;
...
string s1 ="Hello welcome to the world of C sharp";
string s2 = "Hello world welcome to the world of C"; 

 List<string> s1List = (s1 + " " + s2).Split(' ').Distinct().ToList();
于 2012-09-17T18:20:27.480 回答
0

使用框架提供的一些不错的集合操作:

string s1 ="Hello welcome to the world of C sharp";
string s2 = "Hello world welcome to the world of C";

string[] words1 = s1.Split(' ');
string[] words2 = s2.Split(' ');

var s1UniqueWords = words1.Except(words2);
var s2UniqueWords = words2.Except(words1);

var sharedWords = words1.Intersect(words2);

有关各种集合操作的更多信息:http: //msdn.microsoft.com/en-us/library/bb546153.aspx

于 2012-09-17T18:20:43.387 回答
0
public List<string> UniqueWords(string[] setsOfWords)
{
    List<string> words = new List<string>();
    foreach (var setOfWords in setsOfWords)
    {
        words.AddRange(setOfWords.Split(new char[] { ' ' }));
    }
    return words.Distinct().ToList();            
}
于 2012-09-17T18:29:05.520 回答
0

在 C++ 中。假设您有某种拆分字符串的 StringTokenizer 类:

string s1 ="Hello welcome to the world of C sharp";

string s2 = "Hello world welcome to the world of C";

int main( int argc, char* argv[] )
{
    stringTokenizer lStrToken1(s1);
    stringTokenizer lStrToken2(s2);

    vector<string> lVS1 = lStrToken1.getTokens();
    vector<string> lVS2 = lStrToken2.getTokens();

sort( lVS1.begin(), lVS1.end() );
sort( lVS2.begin(), lVS2.end() );
vector<string> lDiff;

set_difference( lVS1.begin(), lVS1.end(), lVS2.begin(), lVS2.end(), 
        inserter( lDiff, lDiff.end() ) );

vector<string>::iterator lIter = lDiff.begin();
for ( ; lIter != lDiff.end(); ++lIter ) {
cout << *lIter << endl;
}

cout << endl;

}

于 2012-09-17T19:14:55.667 回答