2011-05-18 91 views
8
string inputString = "1/10 EP Sp'arrowha?wk XT R;TR 2.4GHz Red"; 
//Characters Collection: (';', '\', '/', ':', '*', '?', ' " ', '<', '>', '|', '&', ''') 
string outputString = "1 10 EP Sp arrowha wk XT R TR 2.4GHz Red"; 
+0

您是否試圖對SQL進行某種轉義? – Qtax 2011-05-18 18:38:12

回答

19

對於下面的代碼披露:

  • 這不是測試
  • 我可能搞砸字符轉義new Regex(...);
  • 我真的不知道C#,但我可以谷歌"C# string replace regex"land on MSDN

    Regex re = new Regex("[;\\/:*?\"<>|&']"); 
    string outputString = re.Replace(inputString, " "); 
    

下面是正確的代碼:

string inputString = "1/10 EP Sp'arrowha?wk XT R;TR 2.4GHz R\\ed"; 
Regex re = new Regex("[;\\\\/:*?\"<>|&']"); 
string outputString = re.Replace(inputString, " "); 
// outputString is "1 10 EP Sp arrowha wk XT R TR 2.4GHz R ed" 

演示:http://ideone.com/hrKdJ

另請參見:http://www.regular-expressions.info/

+1

在char類中移動''',我也會使用'+',比如''[; \\ /:*?\「<> |&'] +」' – Qtax 2011-05-18 18:35:16

+0

@Matt Ball,我理解你可能不開心,OP沒有谷歌第一,但這並不意味着你必須是卑鄙或諷刺。 – Xaisoft 2011-05-18 18:44:22

+0

@ Xaisoft的意思是披露,沒有任何消極的意圖,我已經重寫了它;請讓我知道,如果它仍然出現嚴重 – 2011-05-18 18:45:36