2012-07-20 60 views
0
string[] myArray= { "replay", "answer" }; 
if (myArray.Contains("rểplay")) { 
//... 
} 

此函數將返回false,因爲它嘗試檢查變音符單詞「rplay」而不是「replay」。我如何忽略非間距組合字符,如變音符號並返回true?Array.Contains忽略非間距組合字符,例如變音符

它如何才能反之亦然如下?

string[] myArray= { "rểplay", "answer" }; 
if (myArray.Contains("replay")) { 
//... 
} 

而且如何在這個函數中應用呢?

var ix = Array.FindIndex(myKeys, p => p.Equals(wordIn, StringComparison.CurrentCultureIgnoreCase)); 
wordOut = myKeys[ix]; 
return true; 
+1

見http://stackoverflow.com/questions/249087/how-do-i-remove-diacritics-accents-from-a-string-in-net – 2012-07-20 07:02:36

回答

2

我寧願規範化搜索字符串,並在數組中搜索。

using System.Globalization; 

string input = "rểplay"; 
string decomposed = input.Normalize(NormalizationForm.FormD); 
char[] filtered = decomposed 
    .Where(c => char.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark) 
    .ToArray(); 
string newString = new String(filtered); 

string[] myArray= { "replay", "answer" }; 
if (myArray.Contains(newString)) { 
//... 
} 
+0

看起來不像這應該工作。你沒有去掉變音符號。 – CodesInChaos 2012-07-20 07:20:30

+0

如果反之亦然? string [] myArray = {「rểplay」,「answer」}; if(myArray.Contains(「replay」)){ // ... } – user1437001 2012-07-20 07:22:25

+0

@CodesInChaos,是的,它不適合我。 – user1437001 2012-07-20 07:40:01