C#裡判斷字串是否為中文
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;
}
方法二: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;
}
方法三:/**//// /// 判斷句子中是否含有中文/// /// 字串 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 )
{
}
……
方法六:/**//// /// 給定一個字串,判斷其是否只包含有漢字/// /// /// 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;
}/**//// /// 判斷一個word是否為GB2312編碼的漢字/// /// /// 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;
}
}
}/**//// /// 判斷一個word是否為GBK編碼的漢字/// /// /// 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;
}
}
}/**//// /// 判斷一個word是否為Big5編碼的漢字/// /// /// 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;
}
}
}[@more@]
{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;
}
方法二: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;
}
方法三:/**////
{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 )
{
}
……
方法六:/**////
{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;
}/**////
{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;
}
}
}/**////
{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;
}
}
}/**////
{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;
}
}
}[@more@]
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/631872/viewspace-1012403/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- C#判斷字串是否為日期格式C#字串
- C#實現判斷字元是否為中文C#字元
- C#判斷字串是否合法C#字串
- 判斷字串是否為空字串
- js判斷字串是否為空JS字串
- JavaScript判斷字串是否為空JavaScript字串
- java判斷字串是否為空Java字串
- python 判斷是否為中文Python
- 33判斷字串是否為迴文字串
- J2SE-判斷字串是否為空字串
- YTUOJ-判斷字串是否為迴文字串
- 判斷URL字串是否合法字串
- 判斷字串是否唯一字串
- php判斷字串A是否含有字串BPHP字串
- java判斷輸入的字串是否為數字Java字串
- C 判斷字串是否是子集字串
- PHP 判斷是否包含某字串PHP字串
- Oracle中如何判斷一個字串是否含有漢字(中文)Oracle字串
- 如何判斷一個字串是否為純數字的問題,當然也可以判斷一個字串是否為純字母字串
- js判斷一個變數是否為字串型別JS變數字串型別
- C語言:判斷一個字串是否為迴文C語言字串
- javascript中如何判斷一個字串是否為JSON格式JavaScript字串JSON
- js判斷輸入字串是否為空、空格、null總結JS字串Null
- 在Oracle中,如何判斷一個字串是否為數字?Oracle字串
- 利用ORACLE正規表示式判斷字串是否為日期格式Oracle字串
- js判斷字串中是否含有指定的子字串JS字串
- JS判斷字串是否包含某字串 indexOf()方法使用JS字串Index
- C#之判斷確定檔案是否為程式集C#
- 判斷字串為空方法字串
- js判斷字串中是否含有指定字元JS字串字元
- 判斷是否是字串全都是空格字串
- Java 判斷字串中是否含有漢字.Java字串
- 判斷字串是否是合法的EMail (轉)字串AI
- python判斷是否為listPython
- mysql如何判斷是否為空MySql
- js判斷物件是否為空JS物件
- 判斷是否為迴文字元字元
- 判斷物件值是否為空物件