Methods of C to Judge Character Coding Summarize Six Methods of of
- 2021-10-16 02:27:11
- OfStack
This paper summarizes the method of judging character coding by C # with examples. Share it for your reference, as follows:
Method 1
In the unicode string, the Chinese range is in 4E00... 9FFF: CJK Unified Ideographs.
It is determined whether the character is Chinese or not by judging the unicode code of the character.
protected bool IsChineseLetter(string input,int index)
{
int code = 0;
int chfrom = Convert.ToInt32("4e00", 16); // Scope ( 0x4e00 ~ 0x9fff ) Convert to int ( chfrom ~ chend )
int chend = Convert.ToInt32("9fff", 16);
if (input != "")
{
code = Char.ConvertToUtf32(input, index); // Get a string input The index specified in the index Place character unicode Code
if (code >= chfrom && code <= chend)
{
return true; // When code Returns in Chinese range true
}
else
{
return false ; // When code Returns outside the Chinese range false
}
}
return false;
}
Method 2:
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;
}
Method 3:
/// <summary>
/// Judge whether the sentence contains Chinese Ningxia University Zhang Dong zd4004.blog.163.com
/// </summary>
/// <param > String </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;
}
Method 4:
for (int i=0; i<s.length; i++)
{
Regex rx = new Regex("^[/u4e00-/u9fa5]$");
if (rx.IsMatch(s[i]))
// Yes
else
// No
}
Positive solution!
/u4e00-/u9fa5.
^ [/u4e00-/u9fa5] $Range regularity of Chinese characters
Method 5
unicodeencoding unicodeencoding = new unicodeencoding();
byte [] unicodebytearray = unicodeencoding.getbytes( inputstring );
for( int i = 0; i < unicodebytearray.length; i++ )
{
i++;
// If it is a Chinese character, the high order is not 0
if ( unicodebytearray[i] != 0 )
{
}
...
Method 6
/// <summary>
/// Given 1 The character string is used to judge whether it only contains Chinese characters
/// </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>
/// Judge 1 A word Whether it is GB2312 Coded Chinese characters
/// </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) // Determine whether it is GB2312
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// Judge 1 A word Whether it is GBK Coded Chinese characters
/// </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) // Determine whether it is GBK Code
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// Judge 1 A word Whether it is Big5 Coded Chinese characters
/// </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)) ) // Determine whether it is Big5 Code
{
return true;
}
else
{
return false;
}
}
}
For more readers interested in C # related content, please check the topics on this site: "Summary of XML File Operation Skills in C #", "C # Common Control Usage Tutorial", "WinForm Control Usage Summary", "C # Data Structure and Algorithm Tutorial", "C # Object-Oriented Programming Introduction Tutorial" and "C # Programming Thread Use Skills Summary"
I hope this article is helpful to everyone's C # programming.