簡體   English   中英

在 Java 中刪除部分字符串

[英]Remove part of string in Java

我想從一個字符中刪除一部分字符串,即:

源字符串:

manchester united (with nice players)

目標字符串:

manchester united

有多種方法可以做到。 如果您有要替換的字符串,可以使用String類的replacereplaceAll方法。 如果您要替換子字符串,您可以使用substring API 獲取子substring

例如

String str = "manchester united (with nice players)";
System.out.println(str.replace("(with nice players)", ""));
int index = str.indexOf("(");
System.out.println(str.substring(0, index));

要替換“()”中的內容,您可以使用:

int startIndex = str.indexOf("(");
int endIndex = str.indexOf(")");
String replacement = "I AM JUST A REPLACEMENT";
String toBeReplaced = str.substring(startIndex + 1, endIndex);
System.out.println(str.replace(toBeReplaced, replacement));

字符串替換

String s = "manchester united (with nice players)";
s = s.replace(" (with nice players)", "");

編輯:

按索引

s = s.substring(0, s.indexOf("(") - 1);

使用 String.Replace():

http://www.daniweb.com/software-development/java/threads/73139

例子:

String original = "manchester united (with nice players)";
String newString = original.replace(" (with nice players)","");
originalString.replaceFirst("[(].*?[)]", "");

https://ideone.com/jsZhSC
replaceFirst()可以替換為replaceAll()

使用StringBuilder ,您可以通過以下方式替換。

StringBuilder str = new StringBuilder("manchester united (with nice players)");
int startIdx = str.indexOf("(");
int endIdx = str.indexOf(")");
str.replace(++startIdx, endIdx, "");

您應該使用 String 對象的 substring() 方法。

這是一個示例代碼:

假設:我在這里假設您要檢索字符串直到第一個括號

String strTest = "manchester united(with nice players)";
/*Get the substring from the original string, with starting index 0, and ending index as position of th first parenthesis - 1 */
String strSub = strTest.subString(0,strTest.getIndex("(")-1);

我首先將原始字符串拆分為一個帶有標記“(”的字符串數組,輸出數組位置 0 處的字符串是您想要的。

String[] output = originalString.split(" (");

String result = output[0];

使用 commons lang 中的 StringUtils

空源字符串將返回空值。 空 ("") 源字符串將返回空字符串。 空刪除字符串將返回源字符串。 空 ("") 刪除字符串將返回源字符串。

String str = StringUtils.remove("Test remove", "remove");
System.out.println(str);
//result will be "Test"
// Java program to remove a substring from a string
public class RemoveSubString {

    public static void main(String[] args) {
        String master = "1,2,3,4,5";
        String to_remove="3,";

        String new_string = master.replace(to_remove, "");
        // the above line replaces the t_remove string with blank string in master

        System.out.println(master);
        System.out.println(new_string);

    }
}

如果您只需要刪除“(”之后的所有內容,請嘗試此操作。如果沒有括號,則不執行任何操作。

StringUtils.substringBefore(str, "(");

如果結束括號后可能有內容,請嘗試此操作。

String toRemove = StringUtils.substringBetween(str, "(", ")");
String result = StringUtils.remove(str, "(" + toRemove + ")"); 

要刪除結尾空格,請使用str.trim()

Apache StringUtils函數是空、空和無匹配安全的

科特林解決方案

如果要從末尾刪除特定字符串,請使用removeSuffix文檔

var text = "one(two"
text = text.removeSuffix("(two") // "one"

如果字符串中不存在后綴,則只返回原來的

var text = "one(three"
text = text.removeSuffix("(two") // "one(three"

如果要刪除字符后,請使用

// Each results in "one"

text = text.replaceAfter("(", "").dropLast(1) // You should check char is present before `dropLast`
// or
text = text.removeRange(text.indexOf("("), text.length)
// or
text = text.replaceRange(text.indexOf("("), text.length, "")

您還可以查看removePrefixremoveRangeremoveSurroundingreplaceAfterLast

完整列表在這里:( 文檔

您可以使用replace來修復您的字符串。 以下將返回“(”之前的所有內容,並刪除所有前導和尾隨空格。如果字符串以“(”開頭,它將保持原樣。

str = "manchester united (with nice players)"
matched = str.match(/.*(?=\()/)
str.replace(matched[0].strip) if matched

暫無
暫無

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

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