如何从给定的字符串中删除子字符串?

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

示例:"Hello World!",删除"o""Hell Wrld!"

766846 次浏览

你可以很容易地使用String.replace():

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

查看Apache的stringutil:

  • static String replace(String text, String searchString, String replacement)替换在另一个字符串中出现的所有字符串 李字符串。< / >
  • static String replace(String text, String searchString, String replacement, int max)将一个字符串替换为数组中的另一个字符串 更大的字符串,用于搜索字符串的第一个最大值
  • static String replaceChars(String str, char searchChar, char replaceChar)将字符串中出现的所有字符替换为 李。< / >
  • static String replaceChars(String str, String searchChars, String replaceChars)一次替换字符串中的多个字符。
  • static String replaceEach(String text, String[] searchList, String[] replacementList)替换其中所有出现的Strings 李另一字符串。< / >
  • static String replaceEachRepeatedly(String text, String[] searchList, String[] replacementList)替换所有出现的
  • static String replaceOnce(String text, String searchString, String replacement)将一个字符串替换为数组中的另一个字符串
  • static String replacePattern(String source, String regex, String replacement)替换源String的每个子字符串 使用将给定的正则表达式与给定的替换匹配 这种模式。李DOTALL选项。< / >
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回答,你还可以使用stringutil remove:

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

removeIgnoreCase:

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