Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove specific characters from string in Java

Tags:

java

For example, given a string of Battle of the Vowels:Hawaii vs Gronzy when we specify the characters to be removed as aeiou, the function should transform string to Bttl f th V wls:Hw vs Grzny.

Found this question in the book Programming Interviews Exposed. This was explained in C, however I'm interested in Java.

like image 886
SuperMan Avatar asked Feb 12 '11 02:02

SuperMan


1 Answers

One simple way to do this is to use a regular expression:

"Battle of the Vowels:Hawaii vs Gronzy".replaceAll("[aeiou]","")

Some Java Class Library API documentation:

String.replaceAll: http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#replaceAll(java.lang.String,%20java.lang.String)

Patterns and regular expressions: http://download.oracle.com/javase/1.5.0/docs/api/java/util/regex/Pattern.html#sum

like image 61
Mike Tunnicliffe Avatar answered Sep 21 '22 05:09

Mike Tunnicliffe