簡體   English   中英

如何從給定的字符串中刪除 substring?

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

有沒有一種簡單的方法可以從 Java 中的給定String中刪除 substring?

例子: "Hello World!" , 刪除"o""Hell Wrld!"

您可以輕松使用String.replace()

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

您可以使用 StringBuffer

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

查看Apache StringUtils

  • 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, char searchChar, char replaceChar)替換為另一個。
  • static String replaceChars(String str, String searchChars, String replaceChars)替換字符串中的多個字符。
  • static String replaceEach(String text, String[] searchList, String[] replacementList)替換另一個字符串中所有出現的字符串。
  • 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)使用 Pattern.DOTALL 選項將與給定正則表達式匹配的源字符串的每個子static String replacePattern(String source, String regex, String replacement)為給定的替換。
replace('regex', 'replacement');
replaceAll('regex', 'replacement');

在你的例子中,

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

這對我很有用。

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

或者你可以使用

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)

以上方法將有助於得到答案。

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

您也可以使用 Substring 替換現有字符串:

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

您還可以使用番石榴的CharMatcher.removeFrom函數。

例子:

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

如果你有一些復雜的邏輯來過濾字符,只是另一種方式而不是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() 方法的實現在這里

您可以使用

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 的回答,您還可以使用StringUtils remove

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

removeIgnoreCase

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM