Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove first word from a string in Java

Tags:

java

string

split

What's the best way to remove the first word from a string in Java?

If I have

String originalString = "This is a string";

I want to remove the first word from it and in effect form two strings -

removedWord = "This"
originalString = "is a string;
like image 319
Jim_CS Avatar asked Feb 21 '12 13:02

Jim_CS


2 Answers

You can use substring

removedWord = originalString.substring(0,originalString.indexOf(' '));
originalString = originalString.substring(originalString.indexOf(' ')+1);
like image 121
DonCallisto Avatar answered Oct 04 '22 17:10

DonCallisto


This will definitely a good solution

    String originalString = "This is a string";
    originalString =originalString.replaceFirst("This ", "");
like image 40
Yogesh Avatar answered Oct 04 '22 16:10

Yogesh