2011-07-08 29 views
-3

我有一個問題,下面的程序,它編譯,但是當我運行它說輸入字符串不是在正確的格式。任何人都可以協助C#正則表達式輸入字符串問題

 string path = @"C:/Documents and Settings/expn261/Desktop/CharacterTest/Output.xls"; 
     string strCharater = File.ReadAllText(path,UTF7Encoding.UTF7); 

     strCharater = Regex.Replace(strCharater, "[èéèëêð]", "e"); 
     strCharater = Regex.Replace(strCharater, "[ÉÈËÊ]", "E"); 
     strCharater = Regex.Replace(strCharater, "[àâä]", "a"); 
     strCharater = Regex.Replace(strCharater, "[ÀÁÂÃÄÅ]", "A"); 
     strCharater = Regex.Replace(strCharater, "[àáâãäå]", "a"); 
     strCharater = Regex.Replace(strCharater, "[ÙÚÛÜ]", "U"); 
     strCharater = Regex.Replace(strCharater, "[ùúûüµ]", "u"); 
     strCharater = Regex.Replace(strCharater, "[òóôõöø]", "o"); 
     strCharater = Regex.Replace(strCharater, "[ÒÓÔÕÖØ]", "O"); 
     strCharater = Regex.Replace(strCharater, "[ìíîï]", "i"); 
     strCharater = Regex.Replace(strCharater, "[ÌÍÎÏ]", "I"); 
     strCharater = Regex.Replace(strCharater, "[š]", "s"); 
     strCharater = Regex.Replace(strCharater, "[Š]", "S"); 
     strCharater = Regex.Replace(strCharater, "[ñ]", "n"); 
     strCharater = Regex.Replace(strCharater, "[Ñ]", "N"); 
     strCharater = Regex.Replace(strCharater, "[ç]", "c"); 
     strCharater = Regex.Replace(strCharater, "[Ç]", "C"); 
     strCharater = Regex.Replace(strCharater, "[ÿ]", "y"); 
     strCharater = Regex.Replace(strCharater, "[Ÿ]", "Y"); 
     strCharater = Regex.Replace(strCharater, "[ž]", "z"); 
     strCharater = Regex.Replace(strCharater, "[Ž]", "Z"); 
     strCharater = Regex.Replace(strCharater, "[Ð]", "D"); 
     strCharater = Regex.Replace(strCharater, "[œ]", "oe"); 
     strCharater = Regex.Replace(strCharater, "[Œ]", "Oe"); 
     strCharater = Regex.Replace(strCharater, "[«»\u201C\u201D\u201E\u201F\u2033\u2036]", "\""); 
     strCharater = Regex.Replace(strCharater, "[\u2026]", "..."); 

     string path2 = (@"C:/Documents and Settings/expn261/My Documents/CharacterReplaceTest.csv"); 
     StreamWriter sw = new StreamWriter(path2); 
     sw.WriteLine(strCharater, UTF7Encoding.UTF7); 
+3

是不是說哪個字符串? – Edgar

+1

雖然您的紅線問題非常有趣,但您可能需要考慮將偶然的正確答案標記爲特別正確。它鼓勵其他用戶在他們看到您有能力提出明確和完善的問題並獲得答案時爲您提供幫助。 –

+0

提問沒有足夠的信息,然後沒有響應。 -1 –

回答

3

這不是很知名,但工作像一個魅力。刪除所有變音符號。

// using System.Globalization 
public static string RemoveDiacritics(string s) { 
    s = s.Normalize(NormalizationForm.FormD); 
    StringBuilder sb = new StringBuilder(); 

    for (int i = 0; i < s.Length; i++) { 
     if (CharUnicodeInfo.GetUnicodeCategory(s[i]) != UnicodeCategory.NonSpacingMark) sb.Append(s[i]); 
    } 

    return sb.ToString(); 
} 
1

當異常發生時,編譯器創建一個名爲stack trace束是在異常發生後,要回造成這個異常的第一個方法調用鏈中的所有地方的地址。請參閱此問題存在哪條線路,並嘗試只專注於該線路,而不是檢查整個塊。 :)

2

它看起來像你試圖做的是翻譯字符串中的字符。這是那些情況下,你可能真的只想寫一個大的switch語句之一:

var sb = new StringBuilder(); 
foreach (char c in strCharater) // could you choose a better name than strCharater? 
{ 
    switch (c) 
    { 
     case 'è': 
     case 'é': 
      sb.Append('e'); 
      break; 
     case 'ä': 
     case 'à': 
      break; 
     default: 
      sb.Add(c); 
      break; 
    } 
} 
strCharater = sb.ToString(); 

這種做法有沒有創造萬噸的有被分配和垃圾收集(immutable)的字符串的好處。另外,JIT應該將其編譯成非常快速的代碼!

相關問題