Delete part of a string in Java

I want to remove part of a string from a single character, i.e.:

Source line:

manchester united (with nice players) 

Target line:

 manchester united 
+65
java string
Jan 01 '11 at 19:28
source share
11 answers

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)); 
+139
Jan 01 '11 at 19:35
source share

Replace String

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

Edit:

By index

 s = s.substring(0, s.indexOf("(") - 1); 
+27
Jan 01 '11 at 19:33
source share

Use String.Replace ():

http://www.daniweb.com/software-development/java/threads/73139

Example:

 String original = "manchester united (with nice players)"; String newString = original.replace(" (with nice players)",""); 
+18
Jan 01 '11 at 19:31
source share

I would first split the source string into a String array with the token "(" and the String string at position 0 of the output array is what you would like to have.

 String[] output = originalString.split(" ("); String result = output[0]; 
+6
Jan 01 '11 at 19:36
source share

Using StringBuilder , you can replace the following method.

 StringBuilder str = new StringBuilder("manchester united (with nice players)"); int startIdx = str.indexOf("("); int endIdx = str.indexOf(")"); str.replace(++startIdx, endIdx, ""); 
+6
Jan 03 '12 at 19:05
source share
 originalString.replaceFirst("[(].*?[)]", ""); 

https://ideone.com/jsZhSC
replaceFirst() can be replaced with replaceAll()

+6
Jul 13. '15 at 12:38
source share

You must use the substring () method of the String object.

Here is a sample code:

Assumption: I assume you want to get the string to the first bracket

 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); 
+5
Jan 01 '11 at 19:35
source share

Using StringUtils from commons lang

A string with a null source will return null. An empty string (") returns an empty string. A null delete string returns the original string. An empty string (" ") returns a source string.

 String str = StringUtils.remove("Test remove", "remove"); System.out.println(str); //result will be "Test" 
+2
Dec 14 '17 at 15:09 on
source share
 // 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
Apr 27 '17 at 11:45
source share

You can use replace to fix your string. The following will return everything up to "(", and will also separate all leading and trailing spaces. If the line starts with "(" it will just leave it as it is. "

 str = "manchester united (with nice players)" matched = str.match(/.*(?=\()/) str.replace(matched[0].strip) if matched 
0
Jan 01 '11 at 19:54
source share

If you just need to delete everything after the "(", try this. Does nothing if there are no brackets.

 StringUtils.substringBefore(str, "("); 

If there may be content after the end brackets, try this.

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

To remove spaces use str.trim()

Apache StringUtils functions are null-, empty- and not match- safe

0
Jul 19 '19 at 18:45
source share



All Articles