下面代码已经调试过可行 private void button1_Click(object sender, EventArgs e) { string A = "a好B了"; string LAST = ""; for (int i = 0; i < A.Length; i++) { string B = A.Substring(i, 1); Match m = Regex.Match(B, @"^[A-Z,a-z]*$"); if (!m.Success) { LAST = LAST + "?"; } else { LAST = LAST + B; } } MessageBox.Show(LAST); }
上面提供了判断是否中文字符的方法
Regex reg = new Regex(@"[\u4E00-\u9FFF]");
string str = "测试testing测试aaaaaaaaaaaa"; string res = reg.Replace(str, "?"); Console.WriteLine(res);
Regex reg = new Regex(@"[\u4e00-\u9fa5]");//汉字正则的表达式。
string text1="";
console.write("请输入:");
string text1=console.Readline();
if (reg.IsMatch(text1))//判断是不是汉字的方法,
{
console.write(“你输入法的是汉字\n”);
}
else
{
console.write("你输入的不是汉字“);
}
你根据这个改一下
StringBuilder sb1 = new StringBuilder(100);
for (int i1 = 0; i1 < s1.Length; i1++)
{
char c1=s1[i1];
if ((int)c1 >= 0x4e00 && (int)c1 <= 0x9fa5)
{
c1 = '?';
}
sb1.Append(c1);
}
MessageBox.Show(sb1.ToString());
private void button1_Click(object sender, EventArgs e)
{
string A = "a好B了";
string LAST = "";
for (int i = 0; i < A.Length; i++)
{
string B = A.Substring(i, 1);
Match m = Regex.Match(B, @"^[A-Z,a-z]*$");
if (!m.Success)
{
LAST = LAST + "?";
}
else
{
LAST = LAST + B;
}
}
MessageBox.Show(LAST);
}
input = Encoding.ASCII.GetString(Encoding.ASCII.GetBytes(s)); // input = Hello ?!
string newStr="";
Regex rx = new Regex("^[\u4e00-\u9fa5]$");
for(int i=0;i<str.Length;i++)
{
if(!rx.IsMatch(str[i].ToString()))//不是中文
{
newStr+=str[i];
}
else{
newStr+="?";
}
}
Console.WriteLine(newStr);
如果满足就全部是英文,否则就存在中文
或
判断占字节数呀
public static bool valdateNum(string P_str_num)
{
return Regex.IsMatch(P_str_num,@"^@"[\u4e00-\u9fa5]"$");
}如果返回true 就用“?”代替 即可
public static bool valdateNum(string P_str_num)
{
return Regex.IsMatch(P_str_num,@"^[\u4e00-\u9fa5]$");
}如果返回true 就用“?”代替 即可