C#字符串的全角是指用二个字节来表示的一个字符

  C#字符串的半角是用一个字节来表示的一个字符

  这样的话我们就可以用string.length 和System.text.Encoding.Default.GetByteCount来判断

  其中string.length表示C#字符串字符串的字符数,

  System.text.Encoding.Default.GetByteCount表示字符串的字节数。

  判断半角如下:

 

if (checkString.Length == Encoding.Default.GetByteCount(checkString)) 
{       
   
return true;      
}     
else    
{      
   
return false;     

 

 

判断全角如下:

 

 if (2 * checkString.Length == Encoding.Default.GetByteCount(checkString))  
 {      
   
return true;   
 }     
 
else    
 {      
   
return false;    

 

 

这样就达到了判断C#字符串是全角还是半角的目的。

 

 

相关文章:

  • 2021-11-25
  • 2021-10-06
  • 2021-08-19
  • 2021-11-17
  • 2022-12-23
  • 2021-06-26
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-18
  • 2022-01-15
  • 2022-12-23
相关资源
相似解决方案