zoukankan      html  css  js  c++  java
  • 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;
        }
    }
    

      

  • 相关阅读:
    创建窗口
    文件映射
    匿名管道
    MFC之进度条CProgressCtrl
    跨进程使用句柄和文件操作
    redis安装配置
    git全部操作
    idea中Entity实体中报错:cannot resolve column/table/
    Server returns invalid timezone. Go to 'Advanced' tab and set 'serverTimezon
    sql操作
  • 原文地址:https://www.cnblogs.com/Big-Boss/p/13959070.html
Copyright © 2011-2022 走看看