简体   繁体   中英

Java split string (check given parameters)

For a Java IRC client I have a login funtion. If you type "!LOGIN user pass" it will log you in.

Right now if a user uses a space too much or only uses 1 parameter in stead of "user" + "pass" it will crash the programm due to the way I am spliting the line.

I`m having trouble to find a solution so I can make a check if string user or string pass != null..

Any suggestions would be very much appreciated!

if (line.contains("!LOGIN")){  //!LOGIN username password
  String[] parts = line.split(" ");
  String user = parts[4]; 
  String pass = parts[5];
}
if (line.contains("!LOGIN")){  //!LOGIN username password
  String[] parts = line.split("\\s+");
  String user = parts.length > 3 ? parts[4] : ""; 
  String pass = parts.length > 4 ? parts[5] : "";
}

Use the regex as described in the comments above, then check the size of the array.

In general it is recommended to verify your input before parsing it, or to test if the parsing worked.

In this case you are splitting on string, which gives you no certainty.

The minimum you should do is test if you have enough chunks as expected:

String[] parts = line.split(" ");
if (parts.length >= 5) { 
    // your usual logic 
    String user = parts[4]; 
    String pass = parts[5];
}

But it's generally better to create a pattern that (strictly) defines the acceptable input. You first validate that the input provided matches the expected pattern. (where in your pattern you decide how lenient you want to be)

something like:

public class TestPattern {
    public static String[] inputTest = new String[] {
            "!LOGIN user pass",
            "!LOGIN user pass    ",
            "!LOGIN user     pass",
            "!LOGIN     user pass",
            "    !LOGIN user pass",
            "   !LOGIN    user    pass   "
    };

    public static void main(String[] argv) {
        // ^ = start of line
        // \\s* = 0 or more spaces
        // \\s+ = 1 or more spaces
        // (\\w+) = group 1 containing 1 or more word-characters (a-zA-Z etc)
        // $ = end of line 
        Pattern pattern = Pattern.compile("^\\s*!LOGIN\\s+(\\w+)\\s+(\\w+)\\s*$");
        for (String input : inputTest) {
            Matcher matcher = pattern.matcher(input);
            if (!matcher.find()) {
                System.out.println("input didn't match login: " + input);
                continue;
            }

            String username = matcher.group(1);
            String password = matcher.group(2);

            System.out.println("username[ " + username + " ], password[ " + password + " ]");
        }
    }
}

You can test this also with bad input like:

public static String[] inputFailed = new String[] {
        "",
        "! LOGIN user pass",
        "!LOGINX user pass",
        "!LOGIN user pass other",
        "!LOGIN userpass"
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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