
要检查字符串是否包含特殊字符,您需要使用以下方法 -
Char.IsLetterOrDigit
在 for 循环中使用它并检查是否包含特殊字符的字符串。
假设我们的字符串是 -
string str = "Amit$#%";
现在将字符串转换为字符数组 -
C编写,实现字符串摘要、文件摘要两个功能。里面主要包含3个文件: Md5.cpp、Md5.h、Main.cpp。其中Md5.cpp是算法的代码,里的代码大多是从 rfc-1321 里copy过来的;Main.cpp是主程序。
str.ToCharArray();
使用 for 循环并使用 isLetterOrDigit() 方法检查每个字符。
示例
让我们看看完整的代码。
现场演示
using System;
namespace Demo {
class myApplication {
static void Main(string[] args) {
string str = "Amit$#%";
char[] one = str.ToCharArray();
char[] two = new char[one.Length];
int c = 0;
for (int i = 0; i < one.Length; i++) {
if (!Char.IsLetterOrDigit(one[i])) {
two[c] = one[i];
c++;
}
}
Array.Resize(ref two, c);
Console.WriteLine("Following are the special characters:");
foreach(var items in two) {
Console.WriteLine(items);
}
Console.ReadLine();
}
}
}输出
Following are the special characters: $ # %









