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

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


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

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

查看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.


replace('regex', 'replacement');
replaceAll('regex', 'replacement');

在你的例子中,

String hi = "Hello World!"
String no_o = hi.replaceAll("o", "");

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


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

例子:

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

你可以使用StringBuffer

StringBuffer text = new StringBuffer("Hello World");
text.replace( StartIndex ,EndIndex ,String);

这对我很有用。

String hi = "Hello World!"
String no_o = hi.replaceAll("o", "");

或者你可以用

String no_o = hi.replace("o", "");

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()。


下面是从给定字符串中删除所有子字符串的实现

public static String deleteAll(String str, String pattern)
{
    for(int index = isSubstring(str, pattern); index != -1; index = isSubstring(str, pattern))
        str = deleteSubstring(str, pattern, index);

    return str;
}

public static String deleteSubstring(String str, String pattern, int index)
{
    int start_index = index;
    int end_index = start_index + pattern.length() - 1;
    int dest_index = 0;
    char[] result = new char[str.length()];


    for(int i = 0; i< str.length() - 1; i++)
        if(i < start_index || i > end_index)
            result[dest_index++] = str.charAt(i);

    return new String(result, 0, dest_index + 1);
}

isSubstring()方法的实现在这里


replaceAll(String regex, String replacement)

以上方法将帮助你得到答案。

String check = "Hello World";
check = check.replaceAll("o","");

你也可以使用Substring替换现有的字符串:

var str = "abc awwwa";
var Index = str.indexOf('awwwa');
str = str.substring(0, Index);

如果你知道开始和结束索引,你可以使用它

string = string.substring(0, start_index) + string.substring(end_index, string.length());

你可以使用

String helloWorld = "Hello World";
String target = "e";
String replacement = "";
String replacedString = helloWorld.replace(target, replacement);

The answer is = Hllo World

或者你可以使用正则表达式

String original = "Java is one of best languages. OOP can be used in Java";
String regexTarget = "\\bJava\\b";
String replacedWord = original.replaceAll(regexTarget, "Python");

The answer is = Python is one of best languages. OOP can be used in Python

除了@DwB answer,你还可以使用StringUtils remove:

String hello = "hello world";
String hellYeah = StringUtils.remove(hello, "o");

或removeIgnoreCase:

String hello = "hellO world";
String hellYeah = StringUtils.remove(hello, "o");