Full-width to half-width, Java processing of Chinese

Source: Internet
Author: User

Recently, I encountered a problem of full-width half-width conversion during Chinese processing. So I searched the internet and found that the problem was basically solved, but I found that there was no space in the full-width corner, there is a bug in code for converting the half-width space into the full-width corner. The following is the modified Code: First, what is the difference between the full-width corner and the half-width corner? The width occupies two bytes, and the width occupies one byte. The halfwidth width is mainly used for punctuation. The fullwidth punctuation occupies two bytes and the halfwidth occupies one byte. The Chinese characters still occupy two bytes of Java code, regardless of the halfwidth or fullwidth. next, let's take a look at the conversion code.

 

Import java. Io. unsupportedencodingexception; public class fullcharconverter {public static void main (string [] ARGs) throws unsupportedencodingexception {// returns the string qjstr = "Hello !! Full-width conversion, Dao 53232 "; string result = full2halfchange (qjstr); system. out. println (qjstr); system. out. println (result); system. out. println ("----------------------------------"); // returns the string STR = "Java auto recall 2345"; system. out. println (STR); system. out. println (half2fullchange (STR);} // returns the public static final string full2halfchange (string qjstr) throws unsupportedencodingexception {stringbuffer outstrb UF = new stringbuffer (""); string tstr = ""; byte [] B = NULL; For (INT I = 0; I <qjstr. length (); I ++) {tstr = qjstr. substring (I, I + 1); // convert all-width spaces to half-width spaces if (tstr. equals ("") {outstrbuf. append (""); continue;} B = tstr. getbytes ("Unicode"); // get the Unicode byte data if (B [2] =-1) {// indicates the full angle? B [3] = (byte) (B [3] + 32); B [2] = 0; outstrbuf. append (new string (B, "Unicode");} else {outstrbuf. append (tstr) ;}// end. return outstrbuf. tostring () ;}// returns the fullwidth public static final string half2fullchange (string qjstr) throws unsupportedencodingexception {stringbuffer outstrbuf = new stringbuffer (""); string tstr = ""; byte [] B = NULL; For (INT I = 0; I <qjstr. length (); I ++) {tstr = qjstr. substring (I, I + 1); If (tstr. equals ("") {// halfwidth space outstrbuf. append (tstr); continue;} B = tstr. getbytes ("Unicode"); If (B [2] = 0) {// halfwidth? B [3] = (byte) (B [3]-32); B [2] =-1; outstrbuf. append (new string (B, "Unicode");} else {outstrbuf. append (tstr) ;}} return outstrbuf. tostring ();}}

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.