java is a simple example of judging the length of a Chinese string

  • 2020-06-01 09:51:25
  • OfStack

Without further ado, on the code:


 /**
  *  Gets the length of the string. If there is Chinese, each Chinese character counts as 2 position 
  * @param value  Specified string 
  * @return  Length of string 
  */
  public static int length(String value) {
    int valueLength = 0;
    String chinese = "[\u0391-\uFFE5]";
    /*  Gets the length of the field value and, if Chinese characters are included, the length of each Chinese character 2 , or for 1 */
    for (int i = 0; i < value.length(); i++) {
      /*  To obtain 1 A character  */
      String temp = value.substring(i, i + 1);
      /*  Determine if it is a Chinese character  */
      if (temp.matches(chinese)) {
        /*  The length of Chinese characters is 2 */
        valueLength += 2;
      } else {
        /*  The other character length is 1 */
        valueLength += 1;
      }
    }
    return valueLength;
  }


 /** 
  *  get 1 The length of a string , Display length ,1 The length of Chinese characters or Japanese and Korean characters is 2, The English character length is 1 
  * @param String s  You need to get a string of length  
  * @return int  The length of the string  
  */  
  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; 
  } 


  /** 
  *  get 1 The length of a string , Display length ,1 The length of Chinese characters or Japanese and Korean characters is 1, The English character length is 0.5 
  * @param String s  You need to get a string of length  
  * @return int  The length of the string  
  */  
  public static double getLength(String s) { 
  double valueLength = 0;  
    String chinese = "[\u4e00-\u9fa5]";  
    //  Gets the length of the field value and, if Chinese characters are included, the length of each Chinese character 2 , or for 1  
    for (int i = 0; i < s.length(); i++) {  
      //  To obtain 1 A character   
      String temp = s.substring(i, i + 1);  
      //  Determine if it is a Chinese character   
      if (temp.matches(chinese)) {  
        //  The length of Chinese characters is 1  
        valueLength += 1;  
      } else {  
        //  The other character length is 0.5  
        valueLength += 0.5;  
      }  
    }  
    // Carry integer   
    return Math.ceil(valueLength);  
  }
 According to the length of the content, distinguish between Chinese and English: 

/**
   *  Intercept character length to distinguish Chinese from English 
   *
   * @param abc  String content 
   * @param len  Intercept length 
   * @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();
  }



Related articles: