• JAVA 对String字符串的操作


    判断字符串的格式:

    import java.util.regex.Pattern;
    
    public class StringTest {
    
        public static void main(String[] args) {
            String str = "A从X";
            // 是否全为汉字
            if(str.matches("[u4E00-u9FA5]+")){
                System.out.println("内容是中文");
            }else{
                System.out.println("内容包含非中文");
            }
    
            // 是否全为数字
            if (str.matches("[0-9]*")) {
                System.out.println(true);
            } else {
                System.out.println(false);
            }
    
            // 是否全为小写字母
            if (str.matches("[a-z]*")) {
                System.out.println(true);
            } else {
                System.out.println(false);
            }
    
            // 是否全为大写字母
            if (str.matches("[A-Z]*")) {
                System.out.println(true);
            } else {
                System.out.println(false);
            }
    
            // 是否含有英文
            System.out.println(isENChar(str));
    
            // 是否含有中文
            System.out.println(isCNChar(str));
        }
    
        /*
        是否含有英文
         */
        public static boolean isENChar(String string) {
            boolean flag = false;
            Pattern p = Pattern.compile("[a-zA-z]");
            if(p.matcher(string).find()) {
                flag = true;
            }
            return flag;
        }
    
        /*
        是否含有中文
         */
        public static boolean isCNChar(String string){
            boolean booleanValue = false;
            for(int i=0; i<string.length(); i++){
                char c = string.charAt(i);
                if(c > 128){
                    booleanValue = true;
                    break;
                }
            }
            return booleanValue;
        }
    }
    

      

  • 相关阅读:
    java基础-数组
    泛型 --集合
    单例模式(新)
    static 关键字
    单例模式
    迭代器模式(java版)
    Object类
    JavaWeb学习总结(十七)——JSP中的九个内置对象
    javaweb学习总结(十六)——JSP指令
    javaweb学习总结(十五)——JSP基础语法
  • 原文地址:https://www.cnblogs.com/Big-Boss/p/13959070.html
Copyright © 2020-2023  润新知