有一个简单的方法来删除子字符串从给定的字符串在Java?

例如:“Hello World!”,去掉“o”→“Hell Wrld!”


当前回答

查看Apache StringUtils:

static String replace(String text, String searchString, String replacement) Replaces all occurrences of a String within another String. static String replace(String text, String searchString, String replacement, int max) Replaces a String with another String inside a larger String, for the first max values of the search String. static String replaceChars(String str, char searchChar, char replaceChar) Replaces all occurrences of a character in a String with another. static String replaceChars(String str, String searchChars, String replaceChars) Replaces multiple characters in a String in one go. static String replaceEach(String text, String[] searchList, String[] replacementList) Replaces all occurrences of Strings within another String. static String replaceEachRepeatedly(String text, String[] searchList, String[] replacementList) Replaces all occurrences of Strings within another String. static String replaceOnce(String text, String searchString, String replacement) Replaces a String with another String inside a larger String, once. static String replacePattern(String source, String regex, String replacement) Replaces each substring of the source String that matches the given regular expression with the given replacement using the Pattern.DOTALL option.

其他回答

你应该看看StringBuilder/StringBuffer,它允许你删除,插入,替换指定偏移量的字符。

查看Apache StringUtils:

static String replace(String text, String searchString, String replacement) Replaces all occurrences of a String within another String. static String replace(String text, String searchString, String replacement, int max) Replaces a String with another String inside a larger String, for the first max values of the search String. static String replaceChars(String str, char searchChar, char replaceChar) Replaces all occurrences of a character in a String with another. static String replaceChars(String str, String searchChars, String replaceChars) Replaces multiple characters in a String in one go. static String replaceEach(String text, String[] searchList, String[] replacementList) Replaces all occurrences of Strings within another String. static String replaceEachRepeatedly(String text, String[] searchList, String[] replacementList) Replaces all occurrences of Strings within another String. static String replaceOnce(String text, String searchString, String replacement) Replaces a String with another String inside a larger String, once. static String replacePattern(String source, String regex, String replacement) Replaces each substring of the source String that matches the given regular expression with the given replacement using the Pattern.DOTALL option.

private static void replaceChar() {
    String str = "hello world";
    final String[] res = Arrays.stream(str.split(""))
            .filter(s -> !s.equalsIgnoreCase("o"))
            .toArray(String[]::new);
    System.out.println(String.join("", res));
}

如果你有一些复杂的逻辑来过滤字符,那就用另一种方法代替replace()。

你可以轻松地使用String.replace():

String helloWorld = "Hello World!";
String hellWrld = helloWorld.replace("o","");

你也可以使用番石榴的CharMatcher。removeFrom函数。

例子:

 String s = CharMatcher.is('a').removeFrom("bazaar");