开发者

C# regular expression to strip all but alphabetical and numerical characters from a string?

开发者 https://www.devze.com 2023-01-06 05:21 出处:网络
I\'ve been scratching my head trying to figure out how to use Regex.Replace to take an arbitrary string and return a string that consists of only the alpha-numeric characters of the original string (a

I've been scratching my head trying to figure out how to use Regex.Replace to take an arbitrary string and return a string that consists of only the alpha-numeric characters of the original string (all white space and punctuation rem开发者_JAVA技巧oved).

Any ideas?


var result = Regex.Replace(input, @"[^a-zA-Z0-9]", "");


You could use linq:

string alphanumeric = new String(original.Where(c => Char.IsLetterOrDigit(c)).ToArray());
0

精彩评论

暂无评论...
验证码 换一张
取 消