简体   繁体   English

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

[英]How can I remove a substring from a given String?

Is there an easy way to remove substring from a given String in Java?有没有一种简单的方法可以从 Java 中的给定String中删除 substring?

Example: "Hello World!"例子: "Hello World!" , removing "o""Hell Wrld!" , 删除"o""Hell Wrld!"

You could easily use String.replace() :您可以轻松使用String.replace()

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

You can use StringBuffer您可以使用 StringBuffer

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

Check out Apache StringUtils :查看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)替换另一个字符串中所有出现的字符串。
  • 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 replace(String text, String searchString, String replacement, int max)用更大字符串中的另一个字符串替换一个字符串,作为搜索字符串的第一个最大值。
  • 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, char searchChar, char replaceChar)将字符串中出现的所有static String replaceChars(String str, char searchChar, char replaceChar)替换为另一个。
  • static String replaceChars(String str, String searchChars, String replaceChars) Replaces multiple characters in a String in one go. static String replaceChars(String str, String searchChars, String replaceChars)替换字符串中的多个字符。
  • static String replaceEach(String text, String[] searchList, String[] replacementList) Replaces all occurrences of Strings within another String. static String replaceEach(String text, String[] searchList, String[] replacementList)替换另一个字符串中所有出现的字符串。
  • static String replaceEachRepeatedly(String text, String[] searchList, String[] replacementList) Replaces all occurrences of Strings within another String. static String replaceEachRepeatedly(String text, String[] searchList, String[] replacementList)替换另一个字符串中所有出现的字符串。
  • static String replaceOnce(String text, String searchString, String replacement) Replaces a String with another String inside a larger String, once. static String replaceOnce(String text, String searchString, String replacement)用更大字符串中的另一个字符串替换一个字符串,一次。
  • 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. static String replacePattern(String source, String regex, String replacement)使用 Pattern.DOTALL 选项将与给定正则表达式匹配的源字符串的每个子static String replacePattern(String source, String regex, String replacement)为给定的替换。
replace('regex', 'replacement');
replaceAll('regex', 'replacement');

In your example,在你的例子中,

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

This works good for me.这对我很有用。

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

or you can use或者你可以使用

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

您应该查看StringBuilder/StringBuffer ,它允许您在指定的offset处删除、插入、替换字符。

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

string = string.substring(0, start_index) + string.substring(end_index, string.length());
replaceAll(String regex, String replacement)

Above method will help to get the answer.以上方法将有助于得到答案。

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

You can use Substring also for replacing with existing string:您也可以使用 Substring 替换现有字符串:

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

You can also use guava's CharMatcher.removeFrom function.您还可以使用番石榴的CharMatcher.removeFrom函数。

Example:例子:

 String s = CharMatcher.is('a').removeFrom("bazaar");
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));
}

In case you have some complicated logic to filter the char, just another way instead of replace() .如果你有一些复杂的逻辑来过滤字符,只是另一种方式而不是replace()

Here is the implementation to delete all the substrings from the given string这是从给定字符串中删除所有子字符串的实现

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);
}

The implementation of isSubstring() method is here isSubstring() 方法的实现在这里

You can use您可以使用

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

The answer is = Hllo World

or you can use regex或者你可以使用正则表达式

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

In addition to @DwB answer, you can also use StringUtils remove :除了@DwB 的回答,您还可以使用StringUtils remove

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

or removeIgnoreCase :removeIgnoreCase

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何用给定字符串中的“”替换给定子字符串? - How to replace a given substring with “” from a given string? 如何从字符串中删除子字符串 - How to Remove substring from string 如何从字符串中删除空格? 仅使用indexOf()和substring() - How can I remove spaces from a string? only using indexOf() and substring() 如何从字符串中删除所有出现的 substring - How to remove all occurrences of a substring from a string 如何从 substring 中删除 Substring 开始直到 Java 中的字符串结尾 - How to remove Substring from substring start until the end of the string in Java 如何在Java中使用正则表达式从字符串中提取子字符串? - How can i extract substring from the string using regex in Java? 如何检查字符串是否具有列表中的子字符串? - How can I check if a string has a substring from a List? 如何知道给定的字符串是否是 Java 中另一个字符串的子字符串 - How to know if a given string is substring from another string in Java 如何匹配给定的正则表达式模式以从字符串中获取子字符串 - How to match a given regex pattern to get a substring from a String 如何在 Android Studio 中使用正则表达式从给定的 url 中提取 substring - How can I extract substring from the given url using regex in Android Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM