一个字符串里可以有大写字母,小写字母,数字,标点符号怎么样判断这个字符串只包含两种类型的内容呢?比如
aaa1
aaBB
aaa....a
..1!
谢谢先

解决方案 »

  1.   

        private static boolean isTwoType(String str) {
            char[] chars = str.toCharArray();
            int lowercase = 1;
            int uppercase = 2;
            int number = 4;
            int punctuation = 8;
            int type = 0;
            Pattern pattern = Pattern.compile("\\p{P}");
            for (int i = 0; i < chars.length; i++) {
                if (chars[i] >= '0' && chars[i] <= '9') {
                    type |= number;
                    continue;
                }
                if (chars[i] >= 'a' && chars[i] <= 'z') {
                    type |= lowercase;
                    continue;
                }
                if (chars[i] >= 'A' && chars[i] <= 'Z') {
                    type |= uppercase;
                    continue;
                }
                if (pattern.matcher(chars[i] + "").matches()) {
                    type |= punctuation;
                    continue;
                }
            }
            return Integer.bitCount(type) == 2;
        }
      

  2.   


    对啊,包含任意两种类型的话返回 true,否则返回 false
      

  3.   

    如果是英文就简单,
    \w 单词字符:[a-zA-Z_0-9] 就可以
    但标点符号就麻烦一点,
    要加
    \p{Punct} 标点符号:!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ 
      

  4.   

    你用这个试一下
    Pattern p = Pattern.compile("^([a-zA-Z_0-9]||[\\p{Punct}])*$");
    Matcher m = p.matcher("ffq wer ty uo.");
    System.out.println(m.matches());
      

  5.   

    上面的没加空格判断有点问题
    Pattern p = Pattern.compile("^([a-zA-Z_0-9]||[\\p{Punct}]||[\\p{Blank}])*$");
    Matcher m = p.matcher("地区性This is me!");
    System.out.println(m.matches());上面的代码把中文去掉就可以返加true了.
      

  6.   

    \p{P} 表示所有的 Unicode 标点符号,包括全角、半角的,横排、直排的标点符号。bitCount 是计算一个 int 类型数据中二进制位 1 的数量,比如 13(1101)结果为 3,即“1”的个数有 3 个。算法是 Sun 引用 Hacker's Delight(中文书名《高效程序的奥秘》),采用位运算得出。
      

  7.   

    jdk1.5 doc上面也没说
    \p{P} 表示所有的 Unicode 标点符号,包括全角、半角的,横排、直排的标点符号。 
    你怎么知道的啊?奇怪...
      

  8.   

    这个可不是 Java 规定的,这是 Unicode 组织的标准http://www.unicode.org/reports/tr18/
      

  9.   

    共有 7 种属性(首字母),和一大堆子属性(两个字母)
     
    Lu Letter, Uppercase 
    Ll Letter, Lowercase 
    Lt Letter, Titlecase 
    Lm Letter, Modifier 
    Lo Letter, Other 
    Mn Mark, Nonspacing 
    Mc Mark, Spacing Combining 
    Me Mark, Enclosing 
    Nd Number, Decimal Digit 
    Nl Number, Letter 
    No Number, Other 
    Pc Punctuation, Connector 
    Pd Punctuation, Dash 
    Ps Punctuation, Open 
    Pe Punctuation, Close 
    Pi Punctuation, Initial quote (may behave like Ps or Pe depending on usage) 
    Pf Punctuation, Final quote (may behave like Ps or Pe depending on usage) 
    Po Punctuation, Other 
    Sm Symbol, Math 
    Sc Symbol, Currency 
    Sk Symbol, Modifier 
    So Symbol, Other 
    Zs Separator, Space 
    Zl Separator, Line 
    Zp Separator, Paragraph 
    Cc Other, Control 
    Cf Other, Format 
    Cs Other, Surrogate 
    Co Other, Private Use 
    Cn Other, Not Assigned (no characters in the file have this property)