There are several ways to do this. If you have a string that you want to replace, you can use the replace or replaceAll of the String class. If you want to replace a substring, you can get a substring using the substring API.
for example
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));
To replace the contents inside "()", you can use:
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));
mprabhat Jan 01 '11 at 19:35 2012-01-01 19:35
source share