Daniel Holler Daniel Holler - 7 months ago 10
Java Question

Remove everything in a string after a second space if there is one

Lets say I have this text:

Thundering Blow I
. What I need is something that makes it look like
Thundering Blow
- Removing the roman number at the end of the string.

I have tried
trim()
and some
substring()
methods but they all keep returning:
Thundering Blow I
back to me. Maybe it could remove everything after a
I
in the string? Would also be fine, but I cant seem to find a way to fix it.

Answer

You need this, removes all characters after second space

String s = "Thundering Blow I";
int k = s.indexOf(" ", s.indexOf(" ") + 1);
String res = s.substring(0,k);
System.out.println(res);
Comments