-11

私は2つの別々の文字列を持っています:

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

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

次に、 のような 2 つの文字列の一意の単語を取得します{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()ます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
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

正直なところ、あなたが何をしようとしているのか正確にはわかりませんが、いくつかの可能な答えがあります:

いずれかの文字列にのみ存在する単語を取得します。

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

In C++. Assumes you have some sort of StringTokenizer class that splits the string:

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 に答える