Home > Java > javaTutorial > Simple example of determining the length of Chinese string in java

Simple example of determining the length of Chinese string in java

高洛峰
Release: 2017-01-23 16:12:02
Original
1731 people have browsed it

Without further ado, here’s the code:

/**
  * 获取字符串的长度,如果有中文,则每个中文字符计为2位
  * @param value 指定的字符串
  * @return 字符串的长度
  */
  public static int length(String value) {
    int valueLength = 0;
    String chinese = "[\u0391-\uFFE5]";
    /* 获取字段值的长度,如果含中文字符,则每个中文字符长度为2,否则为1 */
    for (int i = 0; i < value.length(); i++) {
      /* 获取一个字符 */
      String temp = value.substring(i, i + 1);
      /* 判断是否为中文字符 */
      if (temp.matches(chinese)) {
        /* 中文字符长度为2 */
        valueLength += 2;
      } else {
        /* 其他字符长度为1 */
        valueLength += 1;
      }
    }
    return valueLength;
  }
 
 
 /** 
  * 得到一个字符串的长度,显示的长度,一个汉字或日韩文长度为2,英文字符长度为1 
  * @param String s 需要得到长度的字符串 
  * @return int 得到的字符串长度 
  */  
  public static int length(String s) { 
    if (s == null) 
      return 0; 
    char[] c = s.toCharArray(); 
    int len = 0; 
    for (int i = 0; i < c.length; i++) { 
      len++; 
      if (!isLetter(c[i])) { 
        len++; 
      } 
    } 
    return len; 
  } 
 
 
  /** 
  * 得到一个字符串的长度,显示的长度,一个汉字或日韩文长度为1,英文字符长度为0.5 
  * @param String s 需要得到长度的字符串 
  * @return int 得到的字符串长度 
  */  
  public static double getLength(String s) { 
  double valueLength = 0;  
    String chinese = "[\u4e00-\u9fa5]";  
    // 获取字段值的长度,如果含中文字符,则每个中文字符长度为2,否则为1  
    for (int i = 0; i < s.length(); i++) {  
      // 获取一个字符  
      String temp = s.substring(i, i + 1);  
      // 判断是否为中文字符  
      if (temp.matches(chinese)) {  
        // 中文字符长度为1  
        valueLength += 1;  
      } else {  
        // 其他字符长度为0.5  
        valueLength += 0.5;  
      }  
    }  
    //进位取整  
    return Math.ceil(valueLength);  
  }
根据长度截取内容,区分中英文:
 
/**
   * 截取字符长度,区分中英文
   *
   * @param abc 字符串内容
   * @param len 截取长度
   * @return
   */
  public static String subStr(String abc, int len) {
    if (TextUtils.isEmpty(abc) || len <= 0)
      return "";
    StringBuffer stringBuffer = new StringBuffer();
    int sum = 0;
    char[] chars = abc.toCharArray();
    for (int i = 0; i < chars.length; i++) {
      if (sum >= (len * 3)) {
        break;
      }
      char bt = chars[i];
      if (bt > 64 && bt < 123) {
        stringBuffer.append(String.valueOf(bt));
        sum += 2;
      } else {
        stringBuffer.append(String.valueOf(bt));
        sum += 3;
      }
    }
    return stringBuffer.toString();
  }
Copy after login

The above simple example of using Java to determine the length of a Chinese string is all the content shared by the editor. I hope it can give you a reference. I also hope that Please support PHP Chinese website.

For more articles related to simple examples of using Java to determine the length of Chinese strings, please pay attention to the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template