因此,我創(chuàng)建了一些代碼行,可以接受兩個字符串,拆分它們,并將 1 個字符串的每個單詞與另一個字符串進行比較,并表示兩個字符串中都存在相同的單詞,如果是的話,但這是否是比較單詞的有效方法大量文本,談?wù)?300-10000 個單詞,因為 string、split 通過數(shù)組工作,所以它會破壞計算機內(nèi)存嗎?抱歉,我還在學(xué)習(xí) CS 級別,所以幾乎不知道任何術(shù)語。我聽說正則表達式非常擅長這種事情,但它非常令人困惑。static void Main(string[] args){ string text1 = "yeet went the black fox cry went the chicken"; string text2 = "yeet the fox cry the "; string[] spaced1 = text1.Split(" "); string[] spaced2 = text2.Split(" "); for (int s = 0; s < spaced1.Length; s++) { if (spaced1[s]== spaced2[s]) { Console.WriteLine("same word"); Console.WriteLine(spaced1[s]); } } Console.ReadLine();}這個特定的代碼給出了我想要的結(jié)果,我仍然需要使它在逗號和句號等處分開。
2 回答

HUX布斯
TA貢獻1876條經(jīng)驗 獲得超6個贊
如果您必須處理大量單詞,我希望它們存儲在某個文件中。然后你可以使用一個Stream
.?在10000字的情況下,你不必?fù)?dān)心,因為這一次并不是一個很大的數(shù)字。

慕村225694
TA貢獻1880條經(jīng)驗 獲得超4個贊
不完全確定你想在這里實現(xiàn)什么,但假設(shè)這是一個學(xué)習(xí)項目。
您正在做的就是嘗試查找兩個數(shù)組中都存在的項目。為此,您可以使用 Intersect 方法。
string text1 = "yeet went the black fox cry went the chicken";
string text2 = "yeet the fox cry the ";
string[] spaced1 = text1.Split(' ');
string[] spaced2 = text2.Split(' ');
IEnumerable<string> output = spaced1.Intersect(spaced2);
這將創(chuàng)建您想要的輸出。
- 2 回答
- 0 關(guān)注
- 136 瀏覽
添加回答
舉報
0/150
提交
取消