2010-09-02 59 views

回答

6
resultString = Regex.Replace(subjectString, @"[^[email protected]$%^&*()_+=[\]{}';,.-]", "X"); 

應該這樣做。

說明:我複製了您的字符列表並將其粘貼到negated character class[^...])中。我只需進行兩項小修改:轉義右括號(\])並將短劃線放在字符串的末尾。

5
using System.Linq; 
using System.IO; 

string path = ...; 

IEnumerable<char> invalidChars = Enumerable.Concat(
    Path.GetInvalidFileNameChars(), 
    Path.GetInvalidPathChars()); 

foreach (char c in invalidChars) 
{ 
    path = path.Replace(c, ''); // or any char you want 
} 
+0

+1用於指定Path.GetInvalid [FileName | Path] Chars() – SolidRegardless 2012-11-08 10:25:00