有多种方法可以做到。如果你有要替换的字符串,你可以使用String类的replace或replaceAll方法。如果您要替换子字符串,您可以使用子字符串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));