0

如何在不使用正则表达式的情况下编写以下代码?

public static boolean validateCode(String code){
    boolean hasAtLeastOneNumber = Pattern.compile("[0-9].*[0-9]")
                                         .matcher(code).find();

    boolean hasAtLeastTwoLetters = Pattern.compile("[a-zA-Z].*[a-zA-Z]")
                                          .matcher(code).find();

    boolean hasAtLeastOneHyphen = Pattern.compile("-")
                                         .matcher(code).find();
}
4

3 回答 3

4

怎么样

public static boolean validateCode2(String code) {
    int numbers = 0, letters = 0, hyphens = 0;
    for (char c : code.toCharArray()) {
        if (Character.isDigit(c)) numbers++;
        if (Character.isAlphabetic(c)) letters++;
        if (c=='-') hyphens++;
    }

    return numbers>=2 && letters>=2 && hyphens>=1;
}
于 2013-05-19T21:50:00.007 回答
3

对于hasAtLeastOneNumber

for (char c : code.toCharArray()) {
    if (Character.isDigit(c)) {
        return true;
    }
return false;

对于hasAtLeastTwoLetters

int numFound = 0;
for (char c : code.toCharArray()) {
    if (Character.isLetter(c)) {
        numFound++;
        if (numFound >= 2) {
            return true;
        }
    }
}
return false;

对于hasAtLeastOneHyphen

for (char c : code.toCharArray()) {
    if (c == '-') {
        return true;
    }
}
return false;

如果你不想使用toCharArray,你可以使用:

for (int i=0; i<code.length(); i++) {
    char c = code.charAt(i);
    // do the rest of the test here
}

这基本上等同于使用toCharArray,只是它稍微有点令人困惑:查看代码的人需要花一两秒钟才能弄清楚。toCharArray很明显你在做什么。

于 2013-05-19T21:41:44.683 回答
1

您可以遍历字符串并测试它的字符范围。查看IDEONE上的示例,或者询问我是否需要解释。

import java.util.*;
import java.lang.*;

class Main
{
    public static void main (String[] args) throws java.lang.Exception
    {
        System.out.println(validarCodigo("No-numbers"));
        System.out.println(validarCodigo("1-A"));
        System.out.println(validarCodigo("This 1 Matches -- :-)"));
    }

    public static boolean validarCodigo(String codigo) {
        int i;
        char[] chars = codigo.toCharArray();
        char current;
        boolean tieneAlmenosUnNumero = false;
        boolean tieneAlmenosDosLetras = false;
        boolean tieneAlmenosUnGuion = false;

        // Check for at least one number
        for (i=0; i<chars.length; i++) {
            current = chars[i];
            if (current >= '0' && current <= '9') {
                tieneAlmenosUnNumero = true;
                break;
            }
        }

        // Check for at least two letters
        int found = 0;
        for (i=0; i<chars.length; i++) {
            current = chars[i];
            boolean lower = current >= 'a' && current <= 'z';
            boolean upper = current >= 'A' && current <= 'Z';

            if (lower || upper) found++;

            if (found == 2){
                tieneAlmenosDosLetras = true;
                break;
            }
        }

        // Check for at least one hyphen
        for (i=0; i<chars.length; i++) {
            current = chars[i];
            if (current == '-') {
                tieneAlmenosUnGuion = true;
                break;
            }
        }


        return tieneAlmenosUnNumero && tieneAlmenosDosLetras && tieneAlmenosUnGuion;
    }
}
于 2013-05-19T22:02:46.547 回答