在Java中删除字符串的一部分

94
我想从一个字符中删除字符串的一部分,即:
源字符串:
manchester united (with nice players)

目标字符串:
manchester united

5
你如何知道哪些部分需要保留,哪些需要舍弃?如果不知道这一点,就无法回答你的问题。 - Raedwald
12个回答

176

有多种方法可以实现。如果您拥有要替换的字符串,可以使用String类的replacereplaceAll方法。如果您想要替换一个子字符串,可以使用substring API获取子字符串。

例如:

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
也许值得去掉空格:System.out.println(str.replace("(与好玩家一起)", "")); - kiedysktos

36

字符串替换

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

编辑:

按索引

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

3
不要忘记 s = s.replace(...) 的意思是要对字符串s执行替换操作,并将结果赋值回原变量s。请注意,原始字符串s本身不会被更改,而是创建并返回一个新的字符串对象。 - Sergey Kalinichenko
这是不可能的,因为括号中的内容是可以改变的。我需要将字符串从字符中更改。 - zmki
替换函数可以使用,但如果要删除的字符串位于较大字符串的中间,则子字符串版本将无法正常工作。 - Ian

24

我不是那个给你点踩的人,但可能是因为你把“Replace”写错了大小写? - Mike Kwan
1
点赞以抵消恶意的负评(即未经解释且显然无根据的负面评价)。 - Gayot Fow
字符串不一定与给出的示例相同,因此您不能使用替换。 - Confuse
这里有一个性能比较,字符串替换方法在底层使用正则表达式。https://dev59.com/QGQo5IYBdhLWcg3wOdJZ - Igor Vuković

12

8
使用 StringBuilder,您可以用以下方式替换。
StringBuilder str = new StringBuilder("manchester united (with nice players)");
int startIdx = str.indexOf("(");
int endIdx = str.indexOf(")");
str.replace(++startIdx, endIdx, "");

6

首先,我会使用标记“(”将原始字符串拆分为字符串数组,输出数组中的位置0的字符串就是您想要的内容。

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

String result = output[0];

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

2

使用commons lang中的StringUtils

如果源字符串为null,则返回null。如果源字符串为空(""),则返回空字符串。如果要删除的字符串为null,则返回源字符串。如果要删除的字符串为空(""),则返回源字符串。

String str = StringUtils.remove("Test remove", "remove");
System.out.println(str);
//result will be "Test"

2
如果您只需要删除括号后面的所有内容,请尝试以下方法。如果没有括号,则不执行任何操作。"Original Answer"翻译成"最初的回答"。
StringUtils.substringBefore(str, "(");

最初的回答
如果右括号后面可能有内容,请尝试这样做。
String toRemove = StringUtils.substringBetween(str, "(", ")");
String result = StringUtils.remove(str, "(" + toRemove + ")"); 

要去除末尾的空格,请使用str.trim()

Apache StringUtils函数可以处理null、空字符串和无匹配项的情况。


好的解决方案! - Sumit Shukla

2

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, "")

你还可以查看类似的功能,如removePrefixremoveRangeremoveSurroundingreplaceAfterLast
完整列表在这里:文档

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接