Java full-width character half-width character conversion

Source: Internet
Author: User

[Java]View Plaincopy
  1. <summary>
  2. ///Determine if the characters are English half-width characters or punctuation
  3. // </summary>
  4. // <remarks>
  5. /// 32 spaces
  6. /// 33-47 punctuation
  7. /// 48-57 0~9
  8. /// 58-64 punctuation
  9. /// 65-90 a~z
  10. /// 91-96 punctuation
  11. /// 97-122 a~z
  12. /// 123-126 punctuation
  13. // </remarks>
  14. public static bool Isbjchar (char c)
  15. {
  16. int i = (int) C;
  17. return i >= && i <= 126;
  18. }
  19. // <summary>
  20. ///Determines whether the character is full-width character or punctuation
  21. // </summary>
  22. // <remarks>
  23. //<para> Full-width characters-65248 = half-width characters </para>
  24. // <para> Full-width space exceptions </para>
  25. // </remarks>
  26. public static bool Isqjchar (char c)
  27. {
  28. if (c = = ' \u3000 ') return true;
  29. int i = (int) c- 65248;
  30. if (i < ) return false;
  31. return Isbjchar ((char) i);
  32. }
  33. // <summary>
  34. /// convert full-width characters in a string to half-width
  35. // </summary>
  36. public static string Tobj (string s)
  37. {
  38. if (s = = Null | | S.trim () = = string.  Empty) return s;
  39. StringBuilder sb = new StringBuilder (s.length);
  40. For (int i = 0; i < s.length; i++)
  41. {
  42. if (s[i] = = ' \u3000 ')
  43. Sb.  Append (' \u0020 ');
  44. Else if (Isqjchar (S[i]))
  45. Sb.  Append ((char) ((int) s[i]- 65248));
  46. Else
  47. Sb. Append (S[i]);
  48. }
  49. return SB.  ToString ();
  50. }

Full-width spaces are 12288 and half-width spaces are 32

Other character half-width (33-126) and examination. The corresponding relationship between the full angle (65281-65374) is: The difference is 65248


[Java]View Plaincopy
  1. /***************************************
  2. *
  3. * Full angle conversion into half angle
  4. * @param input Raw string
  5. * @return The converted String
  6. *
  7. ***************************************/
  8. Public static string Qtob (String input) {
  9. Char c[] = Input.tochararray ();
  10. for (int i = 0; i < c.length; i++) {
  11. if (c[i] = = ' \u3000 ') {
  12. C[i] = ";
  13. }?
  14. else if (c[i] > ' \uff00 ' && c[i] < ' \uff5f ') {
  15. C[i] = (char) (C[i]- 65248);
  16. }
  17. }
  18. return new String (c);
  19. }
[Java]View Plaincopy
  1. Half angle turn full angle
  2. Public static string Btoq (String input) {
  3. Char c[] = Input.tochararray ();
  4. for ( int i=0; i<c.length;i++) {
  5. if (c[i] = =") {
  6. C[i] = ' \u3000 ';
  7. }
  8. else if (c[i]<' \177 ') {
  9. c[i]= (char) (c[i]+65248);
  10. }
  11. }
  12. return new String (c);
  13. }

Java full-width character half-width character conversion

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.