Java Full-width character turn half-width character

Source: Internet
Author: User

The 1.java code sometimes encounters security problems with code injection, and in order to prevent this, a filtering function is added. The main is to filter the full-width characters, the URL can not recognize the full-width characters converted to half-width characters

1  Public classTest {2      Public Static voidMain (string[] args) {3String t= "Ffac ※00☆№CCC";4 System.out.println (t);5 System.out.println (Tosemiangle (t));6     }7     8     /**9 * Full-width space is 12288, half-width space isTen * Other character half-width (33-126) and full-width (65281-65374) correspondence is: The difference between the 65248 One      *  A * Convert full-width characters in a string to half-width -      * @paramsrc Any string containing full-width to convert -      * @returnstring after conversion the      */ -      Public Staticstring Tosemiangle (string src) { -         Char[] C =Src.tochararray (); -          for(intindex = 0; Index < c.length; index++) { +             if(C[index] = = 12288) {//Full-width spaces -C[index] = (Char) 32; +}Else if(C[index] > 65280 && c[index] < 65375) {//other full-width characters AC[index] = (Char) (C[index]-65248); at             } -         } -         returnstring.valueof (c); -     } -}

Output Result:

FFAC ※00☆№CCC
FFAC ※00☆№CCC

Java Full-width character turn half-width character

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.