例如,给定字符串 Battle of the Vowels:Hawaii vs Gronzy
,当我们将要删除的字符指定为 aeiou
时,函数应将字符串转换为 Bttl f th V wls:Hw vs Grzny
.
在书上找到这个问题Programming Interviews Exposed .这是用 C 语言解释的,但是我对 Java 很感兴趣。
最佳答案
一个简单的方法是使用正则表达式:
"Battle of the Vowels:Hawaii vs Gronzy".replaceAll("[aeiou]","")
一些 Java 类库 API 文档:
String.replaceAll: http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#replaceAll(java.lang.String,%20java.lang.String)
模式和正则表达式:http://download.oracle.com/javase/1.5.0/docs/api/java/util/regex/Pattern.html#sum
关于java - 从 Java 中的字符串中删除特定字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4975978/