String truncation method

Source: Internet
Author: User
String Truncation

Keywords: Java string Chinese interception stringtrimutils, use chararray, Java uses Unicode internally, do not care about Encoding
Java code

  1. /**
  2. * @ Author sunrie
  3. *
  4. */
  5. Public class stringtrimutils {
  6. /**
  7. * The length of a part of a string (Chinese, Japanese, and Korean characters are 2), which is not distinguished between Chinese and English. If the number is not correct, the length should be less than one character bit.
  8. *
  9. * @ Param STR original string
  10. * @ Param specialcharslength (the length of Chinese, Japanese, and Korean characters is 2)
  11. * @ Return
  12. */
  13. Public static string trim (string STR, int specialcharslength ){
  14. If (STR = NULL | "". Equals (STR) | specialcharslength <1 ){
  15. Return "";
  16. }
  17. Char [] chars = Str. tochararray ();
  18. Int charslength = getcharslength (chars, specialcharslength );
  19. Return new string (chars, 0, charslength );
  20. }
  21. /**
  22. * Get the length of a segment of characters. The length of Chinese, Japanese, and Korean characters in the input length is 2, and the length of all characters in the output length is 1.
  23. * @ Param chars a string of characters
  24. * @ Param specialcharslength: the input length. The length of Chinese, Japanese, and Korean characters is 2.
  25. * @ Return: the output length. All characters are 1 characters in length.
  26. */
  27. Private Static int getcharslength (char [] chars, int specialcharslength ){
  28. Int COUNT = 0;
  29. Int normalcharslength = 0;
  30. For (INT I = 0; I <chars. length; I ++ ){
  31. Int specialcharlength = getspecialcharlength (chars [I]);
  32. If (count <= specialcharslength-specialcharlength ){
  33. Count + = specialcharlength;
  34. Normalcharslength ++;
  35. } Else {
  36. Break;
  37. }
  38. }
  39. Return normalcharslength;
  40. }
  41. /**
  42. * Length of the obtained characters: The Chinese, Japanese, and Korean characters are 2 characters in length, and the ASCII code and other characters are 1 characters in length.
  43. * @ Param C character
  44. * @ Return character Length
  45. */
  46. Private Static int getspecialcharlength (char c ){
  47. If (isletter (c )){
  48. Return 1;
  49. } Else {
  50. Return 2;
  51. }
  52. }
  53. /**
  54. * Determines whether a character is an ascill character or other characters (such as Chinese, Japanese, or Korean)
  55. *
  56. * @ Param char C, the character to be judged
  57. * @ Return Boolean: returns the true and ascill characters.
  58. */
  59. Private Static Boolean isletter (char c ){
  60. Int K = 0x80;
  61. Return C/k = 0? True: false;
  62. }
  63. }
Substring, the code found on the Internet, with GBK processing added, there is no problem in UTF-8 and Other encoding states
Java code
  1. Import java. Io. unsupportedencodingexception;
  2. Public class substring {
  3. /**
  4. * Determines whether a character is an ascill character or other characters (such as Chinese, Japanese, or Korean)
  5. *
  6. * @ Param C: The characters to be judged
  7. * @ Return returns true and ascill characters.
  8. */
  9. Public static Boolean isletter (char c ){
  10. Int K = 0x80;
  11. Return C/k = 0? True: false;
  12. }
  13. /**
  14. * Obtain the length of a string. The length of a Chinese character or Japanese/Korean character is 2, and the length of an English character is 1.
  15. *
  16. * @ Param s the string to be obtained.
  17. * @ Return I returns the string length.
  18. */
  19. Public static int length (string s ){
  20. If (S = NULL)
  21. Return 0;
  22. Char [] C = S. tochararray ();
  23. Int Len = 0;
  24. For (INT I = 0; I <C. length; I ++ ){
  25. Len ++;
  26. If (! Isletter (C [I]) {
  27. Len ++;
  28. }
  29. }
  30. Return Len;
  31. }
  32. /**
  33. * The length of a part of a character is not distinguished between Chinese and English characters. If the number is incorrect, the length of a part is equal to one character bit.
  34. *
  35. *
  36. * @ Param origin original string
  37. * @ Param Len truncation length (the length of a Chinese character is calculated as 2)
  38. * @ Param C suffix
  39. * @ Return returns a string
  40. */
  41. Public static string substring (string origin, int Len, string c ){
  42. If (origin = NULL | origin. Equals ("") | Len <1)
  43. Return "";
  44. Byte [] strbyte = new byte [Len];
  45. If (LEN> length (origin )){
  46. Return origin + C;
  47. }
  48. Try {
  49. System. arraycopy (origin. getbytes ("GBK"), 0, strbyte, 0, Len );
  50. Int COUNT = 0;
  51. For (INT I = 0; I <Len; I ++ ){
  52. Int value = (INT) strbyte [I];
  53. If (value <0 ){
  54. Count ++;
  55. }
  56. }
  57. If (count % 2! = 0 ){
  58. Len = (LEN = 1 )? ++ Len: -- Len;
  59. }
  60. Return new string (strbyte, 0, Len, "GBK") + C;
  61. } Catch (unsupportedencodingexception e ){
  62. Throw new runtimeexception (E );
  63. }
  64. }
  65. }

I found it online today at http://sunrie.javaeye.com/blog/115608.

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.