MrPencil
MrPencil

Reputation: 964

Get the substring after particular separator

I an trying to get the number after : of the element string Min:30 to store it in int variable but I am getting a wrong result 5 as Output. How can I get the substring (the number) after : in the element string?

if( element.startsWith("Min:") ) {
    char[] Str = new char[2];
    element.getChars(4, 6, Str, 0);

    String string = str.toString();
    System.out.println(string);
}

Upvotes: 0

Views: 108

Answers (2)

Exorcismus
Exorcismus

Reputation: 2482

Use String.split();

element.split(":");

which returns an array splitted by the specified delimiter

String [] result  = "Min:30".split(":"); //result[1]=30
int value = Integer.parseInt(result[1]);

Upvotes: 1

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521259

int colonIndex = element.indexOf(":");
int value = Integer.parseInt(element.substring(colonIndex + 1, element.length()));

Upvotes: 3

Related Questions