【问题标题】:how to find unique words in two strings? [closed]如何在两个字符串中找到唯一的单词? [关闭]
【发布时间】:2012-09-09 23:59:08
【问题描述】:

我有两个单独的字符串:

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}这样的同一个程序中找到相似的词。

我无法继续。有人可以帮忙吗?

【问题讨论】:

    标签: c# data-structures


    【解决方案1】:

    在 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;
    

    }

    【讨论】:

      【解决方案2】:
      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();            
      }
      

      【讨论】:

        【解决方案3】:

        老实说,我不太确定您的目标是什么,但这里有一些可能的答案:

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

        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();
        

        【讨论】:

          【解决方案4】:

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

          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

          【讨论】:

            【解决方案5】:

            在 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);
            

            【讨论】:

            • 这里不需要拆分选项。
            • @oldrinb 不是在这个确切的例子中,但作为一个通用规则,如果将“句子”标记为“单词”(这 似乎是 OP 的目标),包括在内是个好主意。我将其包含在内,因为如果将其与通用数据一起使用,删除它可能会导致 string.Empty 被包含在结果中。
            【解决方案6】:

            我建议使用Split()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);
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2018-09-26
              • 1970-01-01
              • 2015-12-28
              • 2021-08-29
              相关资源
              最近更新 更多