ITeye博客

字符串截取(中文长度为2)

sunrie · 2007-08-23 11:19 · JSP/Servlet · Markdown

StringTrimUtils,使用charArray,Java内部使用unicode,不用在意编码
java 代码

 

  1. /**
  2. * @author sunrie
  3. *
  4. */
  5. public class StringTrimUtils {
  6. /**
  7. * 截取一段字符的长度(汉、日、韩文字符长度为2),不区分中英文,如果数字不正好,则少取一个字符位
  8. *
  9. * @param str 原始字符串
  10. * @param specialCharsLength 截取长度(汉、日、韩文字符长度为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. * 获取一段字符的长度,输入长度中汉、日、韩文字符长度为2,输出长度中所有字符均长度为1
  23. * @param chars 一段字符
  24. * @param specialCharsLength 输入长度,汉、日、韩文字符长度为2
  25. * @return 输出长度,所有字符均长度为1
  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. * 获取字符长度:汉、日、韩文字符长度为2,ASCII码等字符长度为1
  43. * @param c 字符
  44. * @return 字符长度
  45. */
  46. private static int getSpecialCharLength(char c) {
  47. if (isLetter(c)) {
  48. return 1;
  49. } else {
  50. return 2;
  51. }
  52. }
  53. /**
  54. * 判断一个字符是Ascill字符还是其它字符(如汉,日,韩文字符)
  55. *
  56. * @param char c, 需要判断的字符
  57. * @return boolean, 返回true,Ascill字符
  58. */
  59. private static boolean isLetter(char c) {
  60. int k = 0x80;
  61. return c / k == 0 ? true : false;
  62. }
  63. }

SubString,网上找到的代码,加上了GBK处理,在UTF8等编码状态下也无问题
java 代码

 

  1. import java.io.UnsupportedEncodingException;
  2. public class SubString {
  3. /**
  4. * 判断一个字符是Ascill字符还是其它字符(如汉,日,韩文字符)
  5. *
  6. * @param c 需要判断的字符
  7. * @return 返回true,Ascill字符
  8. */
  9. public static boolean isLetter(char c) {
  10. int k = 0x80;
  11. return c / k == 0 ? true : false;
  12. }
  13. /**
  14. * 得到一个字符串的长度,显示的长度,一个汉字或日韩文长度为2,英文字符长度为1
  15. *
  16. * @param s 需要得到长度的字符串
  17. * @return i得到的字符串长度
  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. * 截取一段字符的长度,不区分中英文,如果数字不正好,则少取一个字符位
  34. *
  35. *
  36. * @param  origin 原始字符串
  37. * @param len 截取长度(一个汉字长度按2算的)
  38. * @param c 后缀
  39. * @return 返回的字符串
  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. }

评论(2)

  1. #2 楼 · raiha · 2011-11-23 16:00

    感谢分享```````

  2. #1 楼 · 天门冬 · 2008-07-31 16:11

    :arrow: :idea: :?:

评论功能维护中,当前仅展示历史评论。