因此,我創建了一些代碼行,可以接受兩個字符串,拆分它們,并將 1 個字符串的每個單詞與另一個字符串進行比較,并表示兩個字符串中都存在相同的單詞,如果是的話,但這是否是比較單詞的有效方法大量文本,談論 300-10000 個單詞,因為 string、split 通過數組工作,所以它會破壞計算機內存嗎?抱歉,我還在學習 CS 級別,所以幾乎不知道任何術語。我聽說正則表達式非常擅長這種事情,但它非常令人困惑。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();}這個特定的代碼給出了我想要的結果,我仍然需要使它在逗號和句號等處分開。
2 回答

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

慕村225694
TA貢獻1880條經驗 獲得超4個贊
不完全確定你想在這里實現什么,但假設這是一個學習項目。
您正在做的就是嘗試查找兩個數組中都存在的項目。為此,您可以使用 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);
這將創建您想要的輸出。
- 2 回答
- 0 關注
- 125 瀏覽
添加回答
舉報
0/150
提交
取消