检查字符串是否包含与 Java 中给定字符不同的字符

Check whether a String does contain different characters as the given one in Java

我有一个修复程序 charset,它是以下一个:

大小写:

A - Z, a-z

数字:

0-9

特殊字符:

Ñ, É, ñ, à, @, £, $, ¥, è, é, ù, ì, ò, _, !, ", #, %, &, ', (, ), * , +, , -, ., /, :, ;, <, =, >, ?, §, `, SPACE, CR, LF, €, [ ], {, |, }, ^ , ~, \, ß,Ä,Ö,Ü,ä,ö,ü

我尝试使用库 Guava 但我的字符串匹配为非 ASCII 字符串:

if(!CharMatcher.ascii().matchesAllOf(myString)){
    //String doesn't match        
}  

我的输入字符串是:

 smsBodyBlock.setBodyContent("A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, Ä, Ö, Ü,a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, ä, ö, ü,0, 1, 2, 3, 4, 5, 6, 7, 8, 9,Ñ, É, ñ, à, @, £, $, ¥, è, é, ù, ì, ò, _, !, , #, %, &, ', (, ), *, +, ,, -, ., /, :, ;, <, =, >, ?, §, `, SPACE, CR, LF, €, [, ], {, |, }, ^, ~, , ß\"");  

所以基本上我上面写的整个字符集。它不匹配 ASCII

是否有任何快速可靠的可扩展方法来检查是否有其他字符而不是我的预定义字符?

我认为最有效的方法之一是 BitSet - 检查字符是否存在的时间复杂度为 O(1)。它大致与使用数组一样高效,但只需要 space.

的八分之一左右
static class MyCustomMatcher {
    // bits needed = last character + 1
    private static final BitSet matcher = new BitSet('ü' + 1);

    static {
        String other = " \r\nÑÉñà@£$¥èéùìò_!\"#%&',()*+-./:;<=>?§`€[]{|}^~\ßÄÖÜäöü";
        matcher.set(c, 'A', 'Z' + 1); // upper
        matcher.set(c, 'a', 'z' + 1); // lower
        matcher.set(c, '0', '9' + 1); // digit
        for (int i = 0; i < other.length(); i++) matcher.set(other.charAt(i));
    }

    public static boolean matchesAll(String s) {
        for (int i = 0; i < s.length(); i++) {
            if (!matcher.get(s.charAt(i))) return false;
        }
        return true;
    }
}

那你可以写

if (MyCustomMatcher.matchesAll("Hello world")) { 
   // do something
}

为了简单起见,我将 class 设为静态,但您可以通过在构造函数中传递要匹配的字符来使其更加灵活和可重用。

这正是 CharMatcher 存在的原因,您已经在使用它,只是没有完全扩展!

唯一的区别是您应该定义自己的字符集。

所以,让我们开始吧:

CharMatcher letters = CharMatcher.inRange('a', 'z')
                  .or(CharMatcher.inRange('A', 'Z'));
CharMatcher numbers = CharMatcher.inRange('0, '9');
CharMatcher specials = CharMatcher.anyOf("ÑÉñà@£$¥èéùìò_!\"#%&'()*+,-./:;<=>?§` \r\n€[]{|}^~\ßÄÖÜäöü");

CharMatcher allMyCharacters = letters.or(numbers).or(specials);

// If you want performance, keep the line below. If not, remove it
allMyCharacters = allMyCharacters.precomputed();

if (allMyCharacters.matchesAllOf(myString)) {
  //
}

确保在某处保留 allMyCharacters 存储字段,例如:

public class MyStringMatcher {
  private static final CharMatcher myCharacters = createCharMatcher();

  private static CharMatcher createCharMatcher() {
    CharMatcher letters = CharMatcher.inRange('a', 'z')
                  .or(CharMatcher.inRange('A', 'Z'));
    CharMatcher numbers = CharMatcher.inRange('0, '9');
    CharMatcher specials = CharMatcher.anyOf("ÑÉñà@£$¥èéùìò_!\"#%&'()*+,-./:;<=>?§` \r\n€[]{|}^~\ßÄÖÜäöü");

    return letters.or(numbers).or(specials).precomputed();
  }

  public static boolean matches(String string) {
    return myCharacters.matchesAllOf(string);
  }
}