Striping out comas in a String

Hi, I have a String from a textbox that should have numbers in it however it will have a coma in it.

is this the best way to strip it out

String string ="123,456";

string= string.substring(0, string.indexOf(","))+ string.substring(string.indexOf(",")+1, string.length());

thanks for you advice

[400 byte] By [Easter1976a] at [2007-11-27 9:13:50]
# 1
Use a regexstring = string.replaceAll("\\,", "");
georgemca at 2007-7-12 22:01:33 > top of Java-index,Java Essentials,New To Java...
# 2
Hi thanks for your help.I am still only on JRE 1.3 String.replaceall is only available in 1.4
Easter1976a at 2007-7-12 22:01:33 > top of Java-index,Java Essentials,New To Java...
# 3

> Hi thanks for your help.

>

> I am still only on JRE 1.3 String.replaceall is only

> available in 1.4

What are the chances!? Ok, sorry about the assumption! How about

public String strip(String string, char charToRemove) {

char[] chars = string.toCharArray();

StringBuffer buf = new StringBuffer();

int len = chars.length;

for(int i = 0; i < len; i++) {

char c = chars[i];

if ( !c == charToRemove) {

buf.append(c);

}

}

return buf.toString();

}

Although I bet someone else has a better method

georgemca at 2007-7-12 22:01:33 > top of Java-index,Java Essentials,New To Java...
# 4
BTW, I think you meant comma, not coma :-)
georgemca at 2007-7-12 22:01:33 > top of Java-index,Java Essentials,New To Java...
# 5
> ...> if ( !c == charToRemove) {Kaj will go mad if he sees this!; )
prometheuzza at 2007-7-12 22:01:33 > top of Java-index,Java Essentials,New To Java...
# 6

> > ...

> > if ( !c == charToRemove) {

>

> Kaj will go mad if he sees this!

> ; )

I noticed it as soon as I hit "post". Good, it'll do him good to cut loose :-)

Kaj try this

public String strip(String string, char charToRemove) {

char[] chars = string.toCharArray();

StringBuffer buf = new StringBuffer();

int len = chars.length;

for(int i = 0; i < len; i++) {

char c = chars[i];

if ( isNotTheCharacterToRemove(c, charToRemove) {

buf.append(c);

}

}

return buf.toString();

}

boolean isNotTheCharacterToRemove(char candidate, char remove) {

return ( !( candidate == remove ) ? Boolean.TRUE.booleanValue() : Boolean.FALSE.booleanValue());

}

*ducks scandinavian ICBMs*

georgemca at 2007-7-12 22:01:33 > top of Java-index,Java Essentials,New To Java...
# 7
> > ...> > if ( !c == charToRemove) {> > Kaj will go mad if he sees this!> ; )Why would he go mad?
Easter1976a at 2007-7-12 22:01:33 > top of Java-index,Java Essentials,New To Java...
# 8
> ... > Why would he go mad?Long story.
prometheuzza at 2007-7-12 22:01:33 > top of Java-index,Java Essentials,New To Java...
# 9
Thanks Guys
Easter1976a at 2007-7-12 22:01:33 > top of Java-index,Java Essentials,New To Java...
# 10

How's this for pointless code? (lifted, as usual, from our actual codebase)

Boolean[] booleans = new Boolean[] { new Boolean(Boolean.TRUE), new Boolean(Boolean.FALSE)};

georgemca at 2007-7-12 22:01:33 > top of Java-index,Java Essentials,New To Java...