user3103246
user3103246

Reputation: 69

Splitting a string in java twice

I want to split the string : "Attack Potion[1]" so that i am left with only the number which is "1" in this case.

I tryed this but it didn't work:

public static void main(String[] args) {
    System.out.println("" + splitter("Attack Potion[1]"));

}

public static int splitter(String i) {
    String[] splitOne = i.split("[");
    String[] splitTwo = splitOne[1].split("]");
    int extractedNumber = Integer.parseInt(splitTwo[0]);
    return extractedNumber;
}

I want the output to be:

1

Upvotes: 0

Views: 9198

Answers (8)

Nipun Talukdar
Nipun Talukdar

Reputation: 5387

Check the snippet below. It should work.

String pattern = "(.*\\[)(\\d+)(\\])";
Pattern regc = Pattern.compile(pattern);
String tomatch = "Attack Potion[1]";
Matcher matcher = regc.matcher(tomatch);
if (matcher.find() && matcher.groupCount() == 3){
    System.out.println(matcher.group(2));
}

Basically, it is better to use regular expressions for doing such stuffs.

Upvotes: 0

Michael Yaworski
Michael Yaworski

Reputation: 13483

For regex expressions, you have to escape the [ and ] characters because they are part of regex delimiters. So you would need to do this:

String[] splitOne = i.split("\\[");
String[] splitTwo = splitOne[1].split("\\]");

However, you don't even need to split it a second time. If you're sure that the number after [ is only one digit, you could simply do this:

public static int splitter(String i) {
    String[] splitOne = i.split("\\[");
    int extractedNumber = Integer.parseInt(splitOne[1].substring(0,1));
    return extractedNumber;
}

Using substring to only parse the first digit.

Upvotes: 0

PakkuDon
PakkuDon

Reputation: 1615

You need to escape the [ and ] characters since they're special characters in regular expressions. You'll need two slashes since a single backslash is used to denote an escape sequence of some sort (ie: \n for newline, etc).

Try the following:

String[] splitOne = i.split("\\[");
String[] splitTwo = splitOne[1].split("\\]");

Alternatively, you could use Pattern.quote.

Example:

String[] splitOne = i.split(Pattern.quote("["));

Upvotes: 2

Christian Tapia
Christian Tapia

Reputation: 34146

I would use this regex:

String string = "Attack Potion[100]";
System.out.println(string.replaceAll("[^0-9]", ""));

Output:

100

Upvotes: 1

Pshemo
Pshemo

Reputation: 124215

split uses regex as parameter and in regex [ or ] are special characters which creates character classes like [a-z]. To make them simple literals you need to escape them. You can do it with "\\[", \\] (actually you don't have to escape ] if it doesn't have unclosed metacharacter [ before. So you can try

String[] splitOne = i.split("\\[");
String[] splitTwo = splitOne[1].split("]");

or even use | (means OR) and add ] to your first split

String[] splitOne = i.split("\\[|]");
int extractedNumber = Integer.parseInt(splitOne[1]);
//                                     ^-use result from splitOne

Upvotes: 1

Nambi
Nambi

Reputation: 12042

you need to escape like this ("\]"),("\[");

public static void main(String[] args) {
        System.out.println("" + splitter("Attack Potion[1]"));

    }

    public static int splitter(String i) {
        String[] splitOne = i.split("\\[");
        String[] splitTwo = splitOne[1].split("\\]");
        int extractedNumber = Integer.parseInt(splitTwo[0]);
        return extractedNumber;
    }

output

1

Upvotes: 0

ccjmne
ccjmne

Reputation: 9618

You should replace your calls to String#split("[") by some calls to split("\\[").

Why do I do split("\\[")?

String#split works with a regex but [ is not a standard character inside a regex. For this to happen, it needs to be escaped, using \[. However, in Java String, \ is not a standard character either, and needs to be escaped as well. Thus, just to split on [, the Java String used is "\\[".

Upvotes: 1

Ajay George
Ajay George

Reputation: 11875

Delimiting worked for me.

String[] splitOne = i.split("\\[");

Upvotes: 0

Related Questions