会有不使用正则表达式的方法吗?

Would there be an approach without using a regular expression?

这是来自 http://www.glassdoor.com/Interview/Indeed-Software-Engineer-Interview-Questions-EI_IE100561.0,6_KO7,24.htm ,具体来说就是问题

"The asked me a method that took in a string and return words up to the max length. There could be any amount of spaces which made it a little tricky "

这是我的解决方案(带有测试用例)

public class MaxLength {
public static void main(String[] args) {
    List<String> allWords = maxWords("Jasmine has no love  for chris", 2);
    for(String word: allWords){
        System.out.println(word);
    }
}
public static List<String> maxWords(String sentence, int length) {
    String[] words = sentence.trim().split("\s+");
    List<String> list = new ArrayList<String>();
    for(String word: words) {
        if(word.length() <= length) {
            list.add(word);
        }
    }
    return list;
}

测试 运行 很好,我得到了预期的输出 - 没有。然而,在实际面试中,我认为面试官并不希望你马上就知道这个正则表达式(我不必从 How do I split a string with any whitespace chars as delimiters? 中找到它) 有没有不使用正则表达式解决这个问题的另一种方法?

您可以使用 StringTokenizer

另一种可能更冗长的方法是遍历字符串并使用字符 class (Character.isWhiteSpace(char c)) 提供的方法并相应地断开字符串。

尝试:

    public static List<String> maxWords(String sentence, int length) {
        List<String> list = new ArrayList<String>();
        String tmp = sentence;
        while (tmp.indexOf(" ") != -1) {
            String word = tmp.substring(0,tmp.indexOf(" "));
            tmp = tmp.substring(tmp.indexOf(" ")+1);
            if(word.length() <= length) {
                list.add(word);
            }
        }
        return list;
    }

好吧,你可以尝试这样的事情: 不会创建很多 subStrings() 只是创建 "matched" 字符串的子字符串

public class Test {

public static void main(String[] args) {
    String s = "Jasmine has no love  for chris";
    s=s.trim();
    List<String> ls = getMaxLength(s, 3);
    for (String str : ls) {
        System.out.println(str);
    }
}

static List<String> getMaxLength(String s, int length) {
    List<String> ls = new ArrayList<String>();
    int i = 0;
    if (s.charAt(i + length) == ' ' || i + length == s.length()) { // search if first word matches your criterion.
        for (i = 1; i < length; i++) { // check for whitespace between 0 and length

            if (s.charAt(i) == ' ') {
                i = length;
                break;
            }
            if (i == length - 1)
                ls.add(s.substring(0, length));
        }
    }

    for (i = length; i < s.length(); i++) {// start search from second word..
        if (s.charAt(i - 1) == ' ' && i + length < s.length() // if char at  length is space or end of String, make sure the char before the current char is a space.  
                && (s.charAt(i + length) == ' ' || i + length == s.length())) {
            for (int j = i; j < i + length; j++) {
                if (s.charAt(j) == ' ') {
                    i = i + length;
                    break;
                }
                if (j == i + length - 1) {
                    ls.add(s.substring(i, i + length));
                }
            }
        }

    }
    return ls;
} // end of method
}