Java replace all square brackets in a string

JavaRegexStringSplit

Java Problem Overview


I want to remove square brackets from a string, but I don't know how.

String str = "[Chrissman-@1]";
str = replaceAll("\\[\\]", "");

String[] temp = str.split("-@");
System.out.println("Nickname: " + temp[0] + " | Power: " + temp[1]);

But my result is: [Chrissman | 1] The square brackets doesn't get removed.

I tried using a different regex: "\\[.*?\\]", "\\[\\d+\\]" but the result is the same, the square brackets still attached on the string.

Edit:

I tried:

str.replaceAll("]", "");
str.replaceAll("[", "");

And now I'm getting:

Exception in thread "Thread-4" java.util.regex.PatternSyntaxException: Unclosed character class near index 0
[
^
	at java.util.regex.Pattern.error(Unknown Source)
	at java.util.regex.Pattern.clazz(Unknown Source)
	at java.util.regex.Pattern.sequence(Unknown Source)
	at java.util.regex.Pattern.expr(Unknown Source)
	at java.util.regex.Pattern.compile(Unknown Source)
	at java.util.regex.Pattern.<init>(Unknown Source)
	at java.util.regex.Pattern.compile(Unknown Source)
	at java.lang.String.replaceAll(Unknown Source)

Java Solutions


Solution 1 - Java

The replaceAll method is attempting to match the String literal [] which does not exist within the String try replacing these items separately.

String str = "[Chrissman-@1]";
str = str.replaceAll("\\[", "").replaceAll("\\]","");

Solution 2 - Java

Your regex matches (and removes) only subsequent square brackets. Use this instead:

str = str.replaceAll("\\[|\\]", "");

If you only want to replace bracket pairs with content in between, you could use this:

str = str.replaceAll("\\[(.*?)\\]", "$1");

Solution 3 - Java

You're currently trying to remove the exact string [] - two square brackets with nothing between them. Instead, you want to remove all [ and separately remove all ].

Personally I would avoid using replaceAll here as it introduces more confusion due to the regex part - I'd use:

String replaced = original.replace("[", "").replace("]", "");

Only use the methods which take regular expressions if you really want to do full pattern matching. When you just want to replace all occurrences of a fixed string, replace is simpler to read and understand.

(There are alternative approaches which use the regular expression form and really match patterns, but I think the above code is significantly simpler.)

Solution 4 - Java

use regex [\\[\\]] -

String str = "[Chrissman-@1]";
String[] temp = str.replaceAll("[\\[\\]]", "").split("-@");
System.out.println("Nickname: " + temp[0] + " | Power: " + temp[1]);

output -

Nickname: Chrissman | Power: 1

Solution 5 - Java

You may also do it like this:

String data = "[yourdata]";
String regex = "\\[|\\]";
data  = data .replaceAll(regex, "");
System.out.println(data);

Solution 6 - Java

I use this regex to replace every string inside brackets:

var= var.replaceAll("\\[\\w+", "[*").replaceAll("\\]", "]");

Solution 7 - Java

Use this line:) String result = strCurBal.replaceAll("[(" what ever u need to remove ")]", "");

    String strCurBal = "(+)3428";
    Log.e("Agilanbu before omit ", strCurBal);
    String result = strCurBal.replaceAll("[()]", ""); // () removing special characters from string
    Log.e("Agilanbu after omit ", result);
   
    o/p :
    Agilanbu before omit : (+)3428
    Agilanbu after omit :  +3428

    String finalVal = result.replaceAll("[+]", ""); // + removing special characters from string
    Log.e("Agilanbu finalVal  ", finalVal);
    o/p
    Agilanbu finalVal : 3428
            
    String finalVal1 = result.replaceAll("[+]", "-"); // insert | append | replace the special characters from string
    Log.e("Agilanbu finalVal  ", finalVal1);
    o/p
    Agilanbu finalVal : -3428  // replacing the + symbol to -

Solution 8 - Java

String str, str1;
Scanner sc = new Scanner(System.in);
   
System.out.print("Enter a String : ");
str = sc.nextLine();
   
  
str1 = str.replaceAll("[aeiouAEIOU]", "");
		   
	      
	             
System.out.print(str1);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionZbarcea ChristianView Question on Stackoverflow
Solution 1 - JavaKevin BowersoxView Answer on Stackoverflow
Solution 2 - JavaBergiView Answer on Stackoverflow
Solution 3 - JavaJon SkeetView Answer on Stackoverflow
Solution 4 - JavaSubhrajyoti MajumderView Answer on Stackoverflow
Solution 5 - JavaZar E AhmerView Answer on Stackoverflow
Solution 6 - JavagOliveiraCView Answer on Stackoverflow
Solution 7 - JavaAgilanbuView Answer on Stackoverflow
Solution 8 - JavaAnmol DevtaView Answer on Stackoverflow