vendredi 6 mars 2015

Removing characters from string efficiently



This may sound like a very simple question but how do you remove multiple different characters from a string without having to write a line for each, which is what I have laboriously done. I have written a string example below:



String word = "Hello, t-his is; an- (example) line."

word = word.replace(",", "");
word = word.replace(".", "");
word = word.replace(";", "");
word = word.replace("-", "");
word = word.replace("(", "");
word = word.replace(")", "");
System.out.println(word);


Which would produce "Hello this is an example line". A more efficient way is?


Thanks




Aucun commentaire:

Enregistrer un commentaire