Alex Rivera | Logout

What are the different methods to parse strings in Java?

Asked 2008-08-05T23:49:53.560
55

For parsing player commands, I've most often used the split method to split a string by delimiters and then to then just figure out the rest by a series of ifs or switches. What are some different ways of parsing strings in Java?

Edit
Report

2 Answers

4

If this is to parse command lines I would suggest using Commons Cli.

The Apache Commons CLI library provides an API for processing command line interfaces.

answered 2008-08-31T01:05:38.457
2

@CodingTheWheel Heres your code, a bit clean up and through eclipse (ctrl+shift+f) and the inserted back here :)

Including the four spaces in front each line.

public static boolean simpleNLP(String inputline, String keywords[]) {
    if (inputline.length() < 1)
        return false;

    List<String> lexed = new ArrayList<String>(); 
    for (String ele : inputline.split(" ")) {
        lexed.add(ele);
    }


    boolean status = false;
    to = 0;
    for (i = 0; i < lexed.size(); i++) {
        String s = (String) lexed.get(i);
        if (s.equalsIgnoreCase(keywords[to])) {
            to++;
            if (to >= keywords.length) {
                status = true;
                break;
            }
        }
    }
    return status;
}
answered 2008-08-06T15:18:18.957

Your Answer