C#中替换中文字符为* C#中把中文字符替换为* 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我用了replace搞不定 replace("???","*") 方法一http://blog.csdn.net/qiujiahao/archive/2007/08/09/1733169.aspx在unicode 字符串中,中文的范围是在4E00..9FFF:CJK Unified Ideographs。 通过对字符的unicode编码进行判断来确定字符是否为中文。 protected bool IsChineseLetter(string input,int index) ...{ int code = 0; int chfrom = Convert.ToInt32("4e00", 16); //范围(0x4e00~0x9fff)转换成int(chfrom~chend) int chend = Convert.ToInt32("9fff", 16); if (input != "") ...{ code = Char.ConvertToUtf32(input, index); //获得字符串input中指定索引index处字符unicode编码 if (code >= chfrom && code <= chend) ...{ return true; //当code在中文范围内返回true } else ...{ return false ; //当code不在中文范围内返回false } } return false; }方法二:http://hi.baidu.com/yhfd/blog/item/3222e1fca22cfb80b901a027.htmlpublic bool IsChina(string CString) { bool BoolValue = false; for (int i = 0; i < CString.Length; i++) { if (Convert.ToInt32(Convert.ToChar(CString.Substring(i, 1))) < Convert.ToInt32(Convert.ToChar(128))) { BoolValue = false; } else { return BoolValue = true; } } return BoolValue; }方法三: /// <summary> /// 判断句子中是否含有中文 宁夏大学 张冬 zd4004.blog.163.com /// </summary> /// <param >字符串</param> public bool WordsIScn(string words) { string TmmP; for (int i = 0; i < words.Length; i++) { TmmP = words.Substring(i, 1); byte[] sarr = System.Text.Encoding.GetEncoding("gb2312").GetBytes(TmmP); if (sarr.Length == 2) { return true; } } return false; }方法四:for (int i=0; i<s.length; i++){Regex rx = new Regex("^[\u4e00-\u9fa5]$");if (rx.IsMatch(s[i]))// 是else// 否}正解!\u4e00-\u9fa5 汉字的范围。^[\u4e00-\u9fa5]$ 汉字的范围的正则方法五unicodeencoding unicodeencoding = new unicodeencoding(); byte [] unicodebytearray = unicodeencoding.getbytes( inputstring ); for( int i = 0; i < unicodebytearray.length; i++ ) { i++; //如果是中文字符那么高位不为0 if ( unicodebytearray[i] != 0 ) { } …… 方法六 /// <summary> /// 给定一个字符串,判断其是否只包含有汉字 /// </summary> /// <param name="testStr"></param> /// <returns></returns> public bool IsOnlyContainsChinese(string testStr) { char[] words = testStr.ToCharArray(); foreach (char word in words) { if ( IsGBCode(word.ToString()) || IsGBKCode(word.ToString()) ) // it is a GB2312 or GBK chinese word { continue; } else { return false; } } return true; } /// <summary> /// 判断一个word是否为GB2312编码的汉字 /// </summary> /// <param name="word"></param> /// <returns></returns> private bool IsGBCode(string word) { byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(word); if (bytes.Length <= 1) // if there is only one byte, it is ASCII code or other code { return false; } else { byte byte1 = bytes[0]; byte byte2 = bytes[1]; if (byte1 >= 176 && byte1 <= 247 && byte2 >= 160 && byte2 <= 254) //判断是否是GB2312 { return true; } else { return false; } } } /// <summary> /// 判断一个word是否为GBK编码的汉字 /// </summary> /// <param name="word"></param> /// <returns></returns> private bool IsGBKCode(string word) { byte[] bytes = Encoding.GetEncoding("GBK").GetBytes(word.ToString()); if (bytes.Length <= 1) // if there is only one byte, it is ASCII code { return false; } else { byte byte1 = bytes[0]; byte byte2 = bytes[1]; if ( byte1 >= 129 && byte1 <= 254 && byte2 >= 64 && byte2 <= 254) //判断是否是GBK编码 { return true; } else { return false; } } } /// <summary> /// 判断一个word是否为Big5编码的汉字 /// </summary> /// <param name="word"></param> /// <returns></returns> private bool IsBig5Code(string word) { byte[] bytes = Encoding.GetEncoding("Big5").GetBytes(word.ToString()); if (bytes.Length <= 1) // if there is only one byte, it is ASCII code { return false; } else { byte byte1 = bytes[0]; byte byte2 = bytes[1]; if ( (byte1 >= 129 && byte1 <= 254) && ((byte2 >= 64 && byte2 <= 126) || (byte2 >= 161 && byte2 <= 254)) ) //判断是否是Big5编码 { return true; } else { return false; } } } 我的目的就是想处理一个字符串string content="sdfgreghf但是开发阶段第三方是dsgfhgfhs";想把中文字符替换成*;变成:content="sdfgreghf**********dsgfhgfhs";如何做呢? 把字符串拆成一个个char字符在判断是否为汉字,是汉字用×代替后拼起来呀 很简单...用正则表达式...return System.Text.RegularExpressions.Regex.Replace(input, "[\u4e00-\u9fa5]", "*"); 已经找到答案:string outstring = System.Text.RegularExpressions.Regex.Replace(inputstring,@"[\u4e00-\u9fa5]+","*");结贴 asp.net如何制作像优酷那样的视频分享功能 net代码转成asp问题 关于在Page里获得MasterPage的html最终内容 [玻璃鱼V]2个小问题 被.net控件给害苦了 连接Access2003数据库出现的问题 软件工程硕士,大家觉得有没有必要读? 高分求助OWC问题,急!!!! 关于客户端的数据校验问题,要求不用VS自带的控件! 请教:如果在ASP.net with C#中获取文件的类型描述 ASP.NET WEB应用程序的问题调试出错 刚载入页面时,js函数如何调用页面控件?
方法一
http://blog.csdn.net/qiujiahao/archive/2007/08/09/1733169.aspx
在unicode 字符串中,中文的范围是在4E00..9FFF:CJK Unified Ideographs。 通过对字符的unicode编码进行判断来确定字符是否为中文。
protected bool IsChineseLetter(string input,int index)
...{
int code = 0;
int chfrom = Convert.ToInt32("4e00", 16); //范围(0x4e00~0x9fff)转换成int(chfrom~chend)
int chend = Convert.ToInt32("9fff", 16);
if (input != "")
...{
code = Char.ConvertToUtf32(input, index); //获得字符串input中指定索引index处字符unicode编码
if (code >= chfrom && code <= chend)
...{
return true; //当code在中文范围内返回true }
else
...{
return false ; //当code不在中文范围内返回false
}
}
return false;
}方法二:
http://hi.baidu.com/yhfd/blog/item/3222e1fca22cfb80b901a027.html
public bool IsChina(string CString)
{
bool BoolValue = false;
for (int i = 0; i < CString.Length; i++)
{
if (Convert.ToInt32(Convert.ToChar(CString.Substring(i, 1))) < Convert.ToInt32(Convert.ToChar(128)))
{
BoolValue = false;
}
else
{
return BoolValue = true;
}
}
return BoolValue;
}方法三: /// <summary>
/// 判断句子中是否含有中文 宁夏大学 张冬 zd4004.blog.163.com
/// </summary>
/// <param >字符串</param>
public bool WordsIScn(string words)
{
string TmmP; for (int i = 0; i < words.Length; i++)
{
TmmP = words.Substring(i, 1); byte[] sarr = System.Text.Encoding.GetEncoding("gb2312").GetBytes(TmmP); if (sarr.Length == 2)
{
return true;
}
}
return false;
}方法四:
for (int i=0; i<s.length; i++)
{
Regex rx = new Regex("^[\u4e00-\u9fa5]$");
if (rx.IsMatch(s[i]))
// 是
else
// 否
}
正解!
\u4e00-\u9fa5 汉字的范围。
^[\u4e00-\u9fa5]$ 汉字的范围的正则方法五
unicodeencoding unicodeencoding = new unicodeencoding();
byte [] unicodebytearray = unicodeencoding.getbytes( inputstring );
for( int i = 0; i < unicodebytearray.length; i++ )
{
i++;
//如果是中文字符那么高位不为0
if ( unicodebytearray[i] != 0 )
{
}
…… 方法六
/// <summary>
/// 给定一个字符串,判断其是否只包含有汉字
/// </summary>
/// <param name="testStr"></param>
/// <returns></returns>
public bool IsOnlyContainsChinese(string testStr)
{
char[] words = testStr.ToCharArray();
foreach (char word in words)
{
if ( IsGBCode(word.ToString()) || IsGBKCode(word.ToString()) ) // it is a GB2312 or GBK chinese word
{
continue;
}
else
{
return false;
}
}
return true;
}
/// <summary>
/// 判断一个word是否为GB2312编码的汉字
/// </summary>
/// <param name="word"></param>
/// <returns></returns>
private bool IsGBCode(string word)
{
byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(word);
if (bytes.Length <= 1) // if there is only one byte, it is ASCII code or other code
{
return false;
}
else
{
byte byte1 = bytes[0];
byte byte2 = bytes[1];
if (byte1 >= 176 && byte1 <= 247 && byte2 >= 160 && byte2 <= 254) //判断是否是GB2312
{
return true;
}
else
{
return false;
}
}
} /// <summary>
/// 判断一个word是否为GBK编码的汉字
/// </summary>
/// <param name="word"></param>
/// <returns></returns>
private bool IsGBKCode(string word)
{
byte[] bytes = Encoding.GetEncoding("GBK").GetBytes(word.ToString());
if (bytes.Length <= 1) // if there is only one byte, it is ASCII code
{
return false;
}
else
{
byte byte1 = bytes[0];
byte byte2 = bytes[1];
if ( byte1 >= 129 && byte1 <= 254 && byte2 >= 64 && byte2 <= 254) //判断是否是GBK编码
{
return true;
}
else
{
return false;
}
}
} /// <summary>
/// 判断一个word是否为Big5编码的汉字
/// </summary>
/// <param name="word"></param>
/// <returns></returns>
private bool IsBig5Code(string word)
{
byte[] bytes = Encoding.GetEncoding("Big5").GetBytes(word.ToString());
if (bytes.Length <= 1) // if there is only one byte, it is ASCII code
{
return false;
}
else
{
byte byte1 = bytes[0];
byte byte2 = bytes[1];
if ( (byte1 >= 129 && byte1 <= 254) && ((byte2 >= 64 && byte2 <= 126) || (byte2 >= 161 && byte2 <= 254)) ) //判断是否是Big5编码
{
return true;
}
else
{
return false;
}
}
}
return System.Text.RegularExpressions.Regex.Replace(input, "[\u4e00-\u9fa5]", "*");
string outstring = System.Text.RegularExpressions.Regex.Replace(inputstring,@"[\u4e00-\u9fa5]+","*");
结贴