Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

more about deleting the first word in a string

807606May 17 2007 — edited May 17 2007
Hi, new question regarding deletion of the first word in a string. i have a string, i'd like to to remove the first word in it, print the new and shorter string, remove the first word in the shorter string and keep going like this until there aren't any more words to remove, i've used this code:
import java.io.*;
 
class Testar {
	public static void main(String[] args) {

		String partDesc = "Hi my name is SandraPandra";
		while (partDesc !="SandraPandra") {
		String newString = partDesc.replaceFirst("^(\\w+)\\s+","");
		
		System.out.println (partDesc);
		partDesc = newString;}
	}
}
i wrote SandraPandra since this is the last word and that's where i'd like it to stop. the thing is, it doesn't , it keeps writing SandraPandra until i interrupt it. anyone understands why this is?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 14 2007
Added on May 17 2007
15 comments
383 views