在Java中删除部分字符串


81

我想从一个字符中删除一部分字符串,即:

源字符串:

manchester united (with nice players)

目标字符串:

manchester united

5
您如何知道保留哪个部分以及丢弃哪个部分?不知道那是不可能回答您的问题?
Raedwald 2013年

Answers:


156

有多种方法可以做到这一点。如果您有要替换的字符串,则可以使用该类的replacereplaceAll方法String。如果您要替换子字符串,则可以使用substringAPI。

例如

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 toBeReplaced = str.substring(startIndex , endIndex + 1);
Yeung 2014年

也值得删除该空间:System.out.println(str.replace(“(有好玩家)”,“”)));
kiedysktos,2016年


19

使用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)","");

我不是拒绝投票的人,但也许是因为您将Replace放在错误的情况下?
Mike Kwan

1
投票反对否决投票(即,无法解释和明显似是而非)的影响。
Gayot Fow 2012年

字符串不必与给出的示例相同。因此,您不能使用replace。
混淆了2015年

这里有性能比较,字符串替换方法在后台使用了正则表达式。stackoverflow.com/questions/16228992/...
伊戈尔·武科维奇


7

使用StringBuilder,您可以替换以下方式。

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


5

您应该使用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);


1
// 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);

    }
}

1

如果只需要删除“(”之后的所有内容,请尝试此操作。如果没有括号,则不执行任何操作。

StringUtils.substringBefore(str, "(");

如果括号后可能有内容,请尝试此操作。

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

要删除末端空格,请使用 str.trim()

Apache StringUtils函数为null,empty和no matchsafe


好的解决方案!!
Sumit Shukla

0

您可以使用它replace来修复您的字符串。下面的代码将返回“(”之前的所有内容,并去除所有前导和尾随空格。如果字符串以“(”开头,则将其保留原样。

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

这似乎在Java中不起作用。它可能是ECMAscript的派生产品。您是否有某种方法可以使此代码像Java中那样工作?
1111161171159459134 2015年

0

Kotlin解决方案

如果要从末尾删除特定的字符串,请使用 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, "")

您还可以检查出removePrefixremoveRangeremoveSurrounding,和replaceAfterLast是相似

完整列表是在这里:文档

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.