Java string processing (full-width to half-width, full-width conversion)

Source: Internet
Author: User

Method 1:
/**
* Special String Conversion
* @ Class Name FullCharConverter
* @ Author v-jiangwei
* @ Create In 2012-8-24
*/
Public class FullCharConverter {

/**
* Conversion functions from full-width to half-width
* @ Methods Name full2HalfChange
* @ Create In 2012-8-24 By v-jiangwei
* @ Param QJstr
* @ Return String
*/
Public static final String full2HalfChange (String QJstr)
{
StringBuffer outStrBuf = new StringBuffer ("");
String Tstr = "";
Byte [] B = null;
For (int I = 0; I <QJstr. length (); I ++ ){
Tstr = QJstr. substring (I, I + 1 );
// Convert fullwidth spaces to halfwidth Spaces
If (Tstr. equals ("")){
OutStrBuf. append ("");
Continue;
}
Try {
B = Tstr. getBytes ("unicode ");
// Obtain unicode bytes
If (B [2] =-1 ){
// Indicates the fullwidth
B [3] = (byte) (B [3] + 32 );
B [2] = 0;
OutStrBuf. append (new String (B, "unicode "));
} Else {
OutStrBuf. append (Tstr );
}
} Catch (UnsupportedEncodingException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}

} // End.
Return outStrBuf. toString ();


}

 

/**
* Halfwidth to fullwidth
* @ Methods Name half2Fullchange
* @ Create In 2012-8-24 By v-jiangwei
* @ Param QJstr
* @ Return String
*/
Public static final String half2Fullchange (String QJstr)
{
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;
}
Try {
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 ();
} Catch (UnsupportedEncodingException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}

}
Return outStrBuf. toString ();
}


/**
* @ Methods Name main
* @ Create In 2012-8-24 By v-jiangwei
* @ Param args
* Void
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
String QJstr = "814 ";
String result = FullCharConverter. full2HalfChange (QJstr );
System. out. println (QJstr );
System. out. println (result );
System. out. println ("------------------------------------");
// Halfwidth to fullwidth
String str = "G45 High Speed ";
System. out. println (str );
System. out. println (FullCharConverter. half2Fullchange (str ));
}


}
Method 2:
/**
* Halfwidth to fullwidth
* @ Param input String.
* @ Return full-width string.
*/
Public static String ToSBC (String input ){
Char c [] = input. toCharArray ();
For (int I = 0; I <c. length; I ++ ){
If (c [I] = ''){
C [I] = '\ u3000 ';
} Else if (c [I] <'\ 177 '){
C [I] = (char) (c [I] + 65248 );


}
}
Return new String (c );
}
 
/**
* Full-width to half-width
* @ Param input String.
* @ Return half-width string
*/
Public static String ToDBC (String input ){
Char c [] = input. toCharArray ();
For (int I = 0; I <c. length; I ++ ){
If (c [I] = '\ u3000 '){
C [I] = ''; www.2cto.com
} Else if (c [I]> '\ uff00' & c [I] <' \ uff5f '){
C [I] = (char) (c [I]-65248 );


}
}
String returnString = new String (c );
Return returnString;
}
Public static void main (String [] args ){
String QJstr = "G45 High Speed ";
String result = StringUtil. ToDBC (QJstr );
System. out. println (result );

}

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.