Java full-width half-width Character conversion Method Example _java

Source: Internet
Author: User


If you figure out the relationship between Full-width and half-width characters in Java
Then the transition between them is not a thing at all.



You can use the following procedure to look at all the characters in Java and the corresponding encoded values





code as follows:
public static void Main (string[] args) { 
for (int i = Character.min_value i <= character.max_value; ++i) { 
SYSTEM.OUT.PRINTLN (i + "" "+ (char) i); 
} 
} 




From the output you can see



1, half-width characters are starting from 33 to 126 end
2, the full-width character corresponding to the half-width character is from 65281 start to 65374 end
3, the half corner of the space is 32. The corresponding Full-width space is 12288
The relationship between Half-width and Full-width is obvious, except that the character offset is 65248 (65281-33 = 65248).



The Java language realizes the conversion between Full-width and Half-width



Understand the relationship between Full-width characters, and then look at the implementation of Java





code as follows:

/** 
* Full-angle string conversion half-corner string 
* 
* @param fullwidthstr 
* Non-empty full-width string 
* @return Half-angle string 
*/ 
private static string Fullwidth2halfwidth (String fullwidthstr) { 
if (null = = Fullwidthstr | | fullwidthstr.length () <= 0) { 
Return ""; 
} 
char[] Chararray = Fullwidthstr.tochararray (); 
Char array traversal of full-width character conversions 
for (int i = 0; i < chararray.length; ++i) { 
int charintvalue = (int) chararray[i]; 
If the conversion relationship is satisfied, the offset between the corresponding subscript is reduced by 65248; if it's a space, just do the conversion. 
if (charintvalue >= 65281 && charintvalue <= 65374) { 
Chararray[i] = (char) (charIntValue-65248); 
else if (Charintvalue = 12288) { 
Chararray[i] = (char) 32; 
} 
} 
return new String (Chararray); 
} 




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.