简体   繁体   English

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

[英]Would there be an approach without using a regular expression?

This is an interview question from http://www.glassdoor.com/Interview/Indeed-Software-Engineer-Interview-Questions-EI_IE100561.0,6_KO7,24.htm , specifically, the problem 这是来自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 " “问我一个方法,该方法接受一个字符串并返回最大长度的单词。可能有任意数量的空格,这使它有点棘手”

Here is my solution(with test case) 这是我的解决方案(带有测试用例)

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;
}

The test ran fine and i got my expected output - no. 测试进行得很好,我得到了预期的输出-不。 However during an actual interview, I think the interviewer doesn't expect you to know this regular expression off the top of your head (I didn't had to find it from How do I split a string with any whitespace chars as delimiters? ) Is there another approach to this problem without using regular expressions? 但是在实际面试中,我认为面试官并不希望您知道这个正则表达式(您不必从我如何用任何空白字符作为分隔符的字符串拆分中找到它)。在不使用正则表达式的情况下,还有另一种方法可以解决此问题吗?

You could use the StringTokenizer . 您可以使用StringTokenizer

Another, maybe more lengthy approach would be to iterate over the string and use the methods provided by the Character class ( Character.isWhiteSpace(char c) ) and break the string accordingly. 另一种可能更长的方法是遍历字符串并使用Character类( Character.isWhiteSpace(char c) )提供的方法并相应地中断字符串。

Try: 尝试:

    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;
    }

Well, you could try something like this : Doesn't create many subStrings() just creates subStrings of "matched" Strings 好吧,您可以尝试执行以下操作:不创建许多subStrings()而是创建“匹配”字符串的子字符串

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM