删除Java中字符串的一部分

删除Java中字符串的一部分,java,string,Java,String,我想从一个字符中删除字符串的一部分,即: 源字符串: manchester united (with nice players) 目标字符串: manchester united 使用String.Replace(): 例如: String original = "manchester united (with nice players)"; String newString = original.replace(" (with nice players)",""); 字符串替换 Str

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

源字符串:

manchester united (with nice players)
目标字符串:

manchester united
使用String.Replace():

例如:

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

字符串替换

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

按索引排序

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

有多种方法可以做到这一点。如果您有要替换的字符串,可以使用
string
类的
replace
replaceAll
方法。如果要替换子字符串,可以使用
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对象的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];

您可以使用
replace
修复字符串。以下命令将返回“(”之前的所有内容,并删除所有前导和尾随空格。如果字符串以“(”开头,它将保持原样

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

使用StringBuilder,可以按以下方式替换

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

replaceFirst()
可以替换为
replaceAll()

null源字符串将返回null。空(“”)源字符串将返回空字符串。null删除字符串将返回源字符串。空(“”)删除字符串将返回源字符串

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

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

StringUtils.substringBefore(str, "(");
如果结尾括号后可能有内容,请尝试此操作

String toRemove = StringUtils.substringBetween(str, "(", ")");
String result = StringUtils.remove(str, "(" + toRemove + ")"); 
要删除端部空间,请使用
str.trim()

函数是空的、空的和无匹配安全的

科特林溶液 如果要从末尾删除特定字符串,请使用
removeSuffix
()

如果字符串中不存在后缀,则只返回原始值

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, "")
您还可以查看、和,它们是相似的


完整列表如下:()

不要忘记
s=s.replace(…)
不可能,因为括号中的内容可能会更改。我需要更改字符中的字符串(替换将工作,但如果要删除的字符串位于较大字符串的中间,子串版本将不起作用。我不是下拉者,但可能是因为您将替换放在错误的情况下。否决投票。字符串不必与给出的示例相同。因此,您不能使用替换。这里有性能比较,字符串替换方法在引擎盖下使用正则表达式。您如何知道保留哪个部分和放弃哪个部分?如果不知道,就不可能回答您的问题?一些拼写错误。should是
String-tobereplace=str.substring(startIndex,endIndex+1);
也值得删除空格:System.out.println(str.replace(“(使用优秀的玩家)”,”);这在Java中似乎不起作用。它可能是ECMAscript的派生。您有什么方法使这段代码在Java中正常工作吗?很好的解决方案!!
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, "")