zoukankan      html  css  js  c++  java
  • java中判断字段真实长度(中文2个字符,英文1个字符)的方法

    public class Char_cn {
     
     public static void main(String[] args) {
      // TODO Auto-generated method stub
      String haha = "我叫兜兜abcd";
      int true_num = String_length(haha);
      System.out.println("true" + true_num);
      int false_num = haha.length();
      System.out.print("flase" + false_num);
     }
     public static int String_length(String value) {
      int valueLength = 0;
      String chinese = "[\u4e00-\u9fa5]";
      for (int i = 0; i < value.length(); i++) {
       String temp = value.substring(i, i + 1);
       if (temp.matches(chinese)) {
        valueLength += 2;
       } else {
        valueLength += 1;
       }
      }
      return valueLength;
     }
    }

     java中判断字段真实长度(中文2个字符,英文1个字符)的方法

    1、判断字符串是否为连续的中文字符(不包含英文及其他任何符号和数字):
    Regex.IsMatch("中文","^[/u4e00-/u9fa5]");
    2、判断字符串是否为中文字符串(仅不包含英文但可以包含其他符号及数字):
    !Regex.IsMatch("中文",@"[a-zA-Z]");

  • 相关阅读:
    十天冲刺之一
    每日日报2021 4/30
    每日日报2021 4/29
    每日日报2021 4/28
    每日日报2021 4/27
    每日日报2021 4/26
    每日日报2021 4/24
    每日日报2021 4/23
    《梦断代码》读后感
    273. Integer to English Words
  • 原文地址:https://www.cnblogs.com/duanxz/p/2696609.html
Copyright © 2011-2022 走看看