even more about deleting the first word in a string

hi, i have this code that removes the first word in a string, returns the shorter string, removes the first word from the shorter string aso... what i would like it to do is to stop when it hits a non-character, but i can't get it to do that. does anyone know why \b won't work?

import java.io.*;

class Testar{

publicstaticvoid main(String[] args){

String partDesc ="Hi my name is SandraPandra.";

while (partDesc.equals("\b") ==false){//this is where something goes wrong

System.out.println(partDesc);

partDesc = partDesc.replaceFirst("^(\\w+)\\s+","");

}

}

}

thanx in advance!

[1183 byte] By [SandraPandraa] at [2007-11-27 4:43:49]
«« Xor
»» Many buttons
# 1
going to bed now, it's almost midnight here... so if someone answers and i don't say a word about it, it's not to be rude- it's just that i'm asleep. good night!
SandraPandraa at 2007-7-12 9:55:38 > top of Java-index,Java Essentials,Java Programming...
# 2
I'd use substring(String,int,int) and indexOf(char) methods!From your code the program will stop only when partDesc = "\b" meaning that if the last "word" in your string is "\b" it will stop!Easier is to use indexOf("\b") and get the substring after it!
pSala at 2007-7-12 9:55:38 > top of Java-index,Java Essentials,Java Programming...
# 3

I believe that this

partDesc = partDesc.replaceFirst("^(\\w+)\\s+", "");

will replace any first word that has whitespace after it, so it won't replace the last word w/ the period and your routine will go on and on. I'm not sure I understand what is the final result you wish to obtain.

petes1234a at 2007-7-12 9:55:38 > top of Java-index,Java Essentials,Java Programming...
# 4

while (partDesc.equals("\b") == false)

That compares partDesc to a string consisting of one backspace character. I suspect you're trying to use the regex word-boundary anchor, but that's a dead end. If you want to stop beheading the string when the regex stops matching, you can write the code exactly that way: class Testar {

public static void main(String[] args) {

String partDesc = "Hi my name is SandraPandra.";

while ( partDesc.matches("^(\\w+)\\s+.*") ) {

partDesc = partDesc.replaceFirst("^(\\w+)\\s+","");

System.out.println(partDesc);

}

}

}

If performance is a concern, you can use a pre-compiled Pattern object for greater efficiency. Thanks to Matcher's lookingAt() method, you can use the same regex for the test and the replacement: class Testar {

public static void main(String[] args) {

String partDesc = "Hi my name is SandraPandra.";

Pattern p = Pattern p = Pattern.compile("^(\\w+)\\s+");

Matcher m = p.matcher(partDesc);

while ( m.lookingAt() ) {

partDesc = m.replaceFirst("");

System.out.println(partDesc);

m.reset(partDesc);

}

}

}

The ^ anchor isn't really necessary in this version, since lookingAt() implicitly anchors the match to the beginning of the string, but you might as well leave it in.

uncle_alicea at 2007-7-12 9:55:38 > top of Java-index,Java Essentials,Java Programming...