Java determines the length of a string containing Chinese

Source: Internet
Author: User

  1. Public Static boolean isletter (char c) {
  2. int k = 0x80;
  3. return c/k = = 0?   true: false;
  4. }
  5. /**
  6. * Determine if the string is empty
  7. * @param str
  8. * @return
  9. */
  10. Public Static boolean isNull (String str) {
  11. if (str==null| | Str.trim (). Equals ("") | | Str.trim (). Equalsignorecase ("null")) {
  12. return true;
  13. }else{
  14. return false;
  15. }
  16. }
  17. /**
  18. * Gets the length of a string, the length of the display, a Chinese character or a Japanese-Korean length of 2, an English character length of 1
  19. * @param string s need to get length strings
  20. * The string length obtained @return int
  21. */
  22. public static int Length (String s) {
  23. if (s = = null)
  24. return 0;
  25. char[] C = S.tochararray ();
  26. int len = 0;
  27. For (int i = 0; i < c.length; i++) {
  28. len++;
  29. if (!isletter (C[i])) {
  30. len++;
  31. }
  32. }
  33. return len;
  34. }
  35. /**  
  36. * Gets the length of a string, the length of the display, a Chinese character or a Japanese-Korean length of 1, an English character length of 0.5
  37. * @param string s need to get length strings
  38. * The string length obtained @return int
  39. */
  40. public static double getlength (String s) {
  41. double valuelength = 0;
  42. String Chinese = "[\u4e00-\u9fa5]";
  43. //Gets the length of the field value, if it contains Chinese characters, the length of each Chinese character is 2, otherwise 1
  44. For (int i = 0; i < s.length (); i++) {
  45. //Get a character
  46. String temp = s.substring (i, i + 1);
  47. //Determine if it is a Chinese character
  48. if (temp.matches (Chinese)) {
  49. //Chinese character length is 1
  50. Valuelength + = 1;
  51. } Else {
  52. //Other character length is 0.5
  53. Valuelength + = 0.5;
  54. }
  55. }
  56. // Carry rounding
  57. return Math.ceil (valuelength);
  58. }
  59. Reprint Source: http://blog.csdn.net/z69183787/article/details/18843925

Java determines the length of a string containing Chinese

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.