zoukankan      html  css  js  c++  java
  • Java_Character类

      Character类用于对单字符进行操作。

    常用的方法:

     System.out.println(Character.isDigit('1')); // true  判断是否是一个数字字符
     System.out.println(Character.isLetter('a')); //true  判断是否是一个字母
     System.out.println(Character.isDefined('我')); //true  确定字符是否在Unicode中定义
     System.out.println(Character.isWhitespace(',')); //false  确定指定的字符是否为空格
     System.out.println(Character.isUpperCase('a')); //false  确定指定的字符是否大写
     System.out.println(Character.isLowerCase('A')); //false  确定指定的字符是否小写
     System.out.println(Character.toUpperCase('a')); // A  确定指定的字符的大写形式
     System.out.println(Character.toLowerCase('A')); // a  确定指定的字符的小写形式
    

    实例:

    判断字符串中是否含有数字,并打印该数字

    • 方法一
     String a = "A1B2C3a0";
     char[] c = new char[a.length()];
     for (int i=0; i< a.length(); i++) {
         c[i] = a.charAt(i); // 将 String 类型转换为 char 类型
         if(Character.isDigit(c[i])){
             System.out.println(c[i]+" 是数字");
         }
     }
    
    • 方法二
     String a = "A1B2C3a0";
     char[] c = a.toCharArray(); // 将 String 类型字符串转换为 char 类型数组
     for(char i: c){
         if(Character.isDigit(i)){
             System.out.println(i+" 是数字");
         }
     }
    
    • 方法三
     String a = "A1B2C3a0";
      for(int i=0;i<a.length();i++){
          String s = a.substring(a.length()-1-i);  // 从最后一位开始往前截取
          if(Character.isDigit(s.charAt(0))){
              System.out.println(s.charAt(0)+" 是数字");
          }
      }
    

    以上三种方法将字符串转换为字符类型进行判断。还可以通过正则表达式进行验证字符串中是否含有数字。

     String a = "A1B2C3a05";
     Matcher m = Pattern.compile("\d").matcher(a);
     while(m.find()){
         System.out.println(m.group()+ " 是数字");
     }
    
    
  • 相关阅读:
    pandas 的pd.cut()数据分箱
    pandas 的groupby()
    pandas 的DataFrame.apply()
    天池二手车_特征工程
    numpy简单的笔记
    python 面向对象编程的@property
    mybatis 复杂sql语句
    mybatis Lombok
    mybatis 获取 sqlSession
    mybatis @Param 注解
  • 原文地址:https://www.cnblogs.com/zeo-to-one/p/9348414.html
Copyright © 2011-2022 走看看