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
> 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
> > ...
> > 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*
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)};