zoukankan      html  css  js  c++  java
  • 【笔记】分离字符串中的数字、字母和汉字


    package com.xtl.test;

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    /**
     * 字符串中分离数字和字母
     * @author xiatianlong
     *
     * @date 2016年4月28日 上午9:38:24
     */
    public class Test {

        public static void main(String[] args) {

            StringBuffer characters = new StringBuffer();
            StringBuffer letter = new StringBuffer();
            StringBuffer number = new StringBuffer();
            
            String str  = "中国abc123";
            char[] strArr = str.toCharArray();
            for (char string : strArr) {
                // 判断是否为字母
                if ((string+"").matches("[a-z]") || (string+"").matches("[A-Z]")){
                    letter.append(string+"  ");
                }
                // 判断是否为数字
                if ((string+"").matches("[0-9]")){
                    number.append(string+"  ");
                }
                // 判断是否为汉字
                if (isChineseChar(string+"")){
                    characters.append(string+"  ");
                }
            }
            System.out.println(characters.toString());
            System.out.println(letter.toString());
            System.out.println(number.toString());
        }
        
        /**
         * 判断是否为汉字
         * @param str
         *             字符串
         * @return
         */
        public static boolean isChineseChar(String str){
               boolean temp = false;
               Pattern p=Pattern.compile("[u4e00-u9fa5]");
               Matcher m=p.matcher(str);
               if(m.find()){
                   temp =  true;
               }
               return temp;
           }
    }

  • 相关阅读:
    Sum Root to Leaf Numbers
    Sum Root to Leaf Numbers
    Sort Colors
    Partition List
    Binary Tree Inorder Traversal
    Binary Tree Postorder Traversal
    Remove Duplicates from Sorted List II
    Remove Duplicates from Sorted List
    Search a 2D Matrix
    leetcode221
  • 原文地址:https://www.cnblogs.com/mybug/p/5441639.html
Copyright © 2011-2022 走看看