How can I split a string by it's datatype in java?

Jacob :

In my plugin for Spigot, a player enters a command such as /logging chat clear 1d. The argument number 2 (3rd arg) which is 1d needs to be parsed for date (e.g. 1d = 1 day, 15m = 15 minutes). I've figured out the parsing part, but when I try to parse more than one number (1 vs 11) my parsing doesn't work because I split based on characters, not based on if its an integer or string. I do String[] part = arg3.split(""); then take the first character as the number and second as the string. How can I do this but split so that I can have multiple numbers? (regex?)

omoshiroiii :
private void myMethod() {
    String integers = "";
    String characters = "";
    String splitArgument = ""; //this is the 1d or 11d part
    for(int x = 0; x < splitArgument.length(); x++) {
        Char currentChar = splitArgument.charAt(x);
        if(Character.isDigit(currentChar)) {
            integers += currentChar;
        }else {
            characters += currentChar;
        }
    }
}

Where myMethod just represents the area of the code you are analyzing the input in. You can create a method (as isInteger()) that takes the characters from the String you are checking and determines whether they are Integers/String and re-concatenate Strings for them. For the integer part you could then do:

int myInteger = Integer.parseInt(integers);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=327485&siteId=1
Recommended