Home > OS >  JAVA - How to get text after a particular character?
JAVA - How to get text after a particular character?

Time:11-20

I have a String Chocolate:30:2 in a variable and I want to extract the number after the second colon i.e. 2. So, How can I extract that number?

CodePudding user response:

For example:

String s = "Chocolate:30:2";
String number = s.split(":")[2];

CodePudding user response:

If the second colon is actually the last colon, you can use:

String after = str.substring(1   str.lastIndexOf(':'));

CodePudding user response:

You can use String lastIndexOf method.

String result = str.substring(str.lastIndexOf(':')   1);
  • Related