C# 判断字符编码的六种方法
方法一
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;
}
}
}
C# 判断字符编码的六种方法的更多相关文章
- 用chardet判断字符编码的方法
转自http://www.cnblogs.com/xiaowuyi/archive/2012/03/09/2387173.html 用chardet判断字符编码的方法 1.chardet下载与安装 ...
- python 判断字符编码
一般情况下,需要加这个: import sys reload(sys) sys.setdefaultencoding('utf-8') 打开其他文件编码用codecs.open 读 下面的代码读取了文 ...
- XE Delphi 判断字符为中文的方法
在uses中添加System.AnsiStrings /// Param ch--字符串/// Param cno--字符位置 function IsZHChar(const ch: AnsiStri ...
- PHP 判断字符的编码 并输出想要的编码格式字符 (转)
/** * 判断字符编码 并输出想要的编码 * Enter description here ... * @param unknown_type $string * @param unknown_t ...
- Servlet字符编码过滤器,实现图书信息的添加功能,避免产生文字乱码现象的产生
同样的代码,网上可以找到和我一模一样的代码和配置,比我的更加详细,但是我重新写一个博客的原因自是把错误的原因写出来,因为这就是个坑,我弄了一天,希望对你们有所帮助.只为初学者发现错误不知道怎么解决有所 ...
- Android中判断字符是否为中文、韩文、日文
我们经常需要在程序中判断一个字符是否为CJK(Chinese.Japanese.Korean)语言的字符. 例如,在Contacts里面程序需要判断联系人姓名的所属语言. 今天为大家介绍一种NameS ...
- PHP爬虫(3)PHP DOM开源代码里的大坑和字符编码
一.开源代码的问题 在PHP爬虫(2)中介绍了开源工程Sunra.PhpSimple.HtmlDomParser.在实际工作中发现一个问题,例如http://www.163.com的网页数据怎么也抓取 ...
- C#三种判断字符是否为汉字的方法
判断一个字符是不是汉字通常有三种方法,第一种用 ASCII 码判断,第二种用汉字的 UNICODE 编码范围判 断,第三种用正则表达式判断,以下是具体方法. 1.用ASCII码判断 在 ASCII码表 ...
- (转)java判断string变量是否是数字的六种方法小结
java判断string变量是否是数字的六种方法小结 (2012-10-17 17:00:17) 转载▼ 标签: it 分类: 转发 1.用JAVA自带的函数 public static boolea ...
随机推荐
- 第9月第5天 AVVideoAverageBitRateKey
1. https://stackoverflow.com/questions/11751883/how-can-i-reduce-the-file-size-of-a-video-created-wi ...
- 判断gps是否在国内
参考文章:[WP7]判断GPS坐标是否在中国 根据国家行政边界判定(光线投射算法) 按需求调整了原文中的部分边界值,测试几组边界附近内外坐标,结果较为准确. /** * 判断GPS坐标是否在多边形中 ...
- (F - 超级英雄Hero HYSBZ - 1191 )匈牙利算法
题目链接:https://cn.vjudge.net/contest/281037#problem/F 题目大意:中文题目 具体思路:可以看成二分图匹配,寻找最大匹配就可以了,注意当某一个匹配不到的时 ...
- (A - 整数划分 HYSBZ - 1263)(数组模拟大数乘法)
题目链接:https://cn.vjudge.net/problem/HYSBZ-1263 题目大意:中文题目 具体思路:先进了能的拆成3,如果当前剩下的是4,就先不减去3,直接乘4,如果还剩2的话, ...
- D. Dasha and Chess(交互题)
题目链接:http://codeforces.com/contest/1100/problem/D 题目大意:给你一个999*999的图,然后有666个黑色旗子,一个白色棋子,每一次白色棋子只能在它附 ...
- Shell-遍历删除指定目录
Code: find $LibPath/ -name .svn | xargs rm -rf
- Jquery ajax json 不执行success的原因 坑爹
最近在看jQuery的API文档,在使用到jQuery的ajax时,如果指定了dataType为json,老是不执行success回调,而是执行了error回调函数,极度郁闷.后面改为1.2.6版本可 ...
- STS热部署,springboot项目中修改代码不用重新启动服务
方法如下: 1.在pom文件中引入 devtools 依赖: <dependency> <groupId>org.springframework.boot</grou ...
- [SDOI2009]HH去散步 「矩阵乘法计数」
计数问题也许可以转化为矩阵乘法形式 比如若该题没有不能在一条边上重复走的条件限制,那么直接将邻接矩阵转化为矩阵乘法即可 故 矩阵乘法计数 对于计数问题,若可以将 \(n\) 个点表示成 \(n \ti ...
- logback.xml 模板
ssm模板 <?xml version="1.0" encoding="UTF-8"?> <!--configuration 根节点,包含下 ...