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;
        }
    }
    

      

  • 相关阅读:
    java类研究(String)
    webservices
    LoadRunner(软件性能测试工具)
    java线程
    lucene solr
    java IO
    实现一个可变长数组
    [北大程序设计与算法]--虚函数与多态的实例
    A1155 Heap Paths [堆的dfs]
    A1154 Vertex Coloring
  • 原文地址:https://www.cnblogs.com/Big-Boss/p/13959070.html
Copyright © 2011-2022 走看看