Below is the example where I want to remove the square brackets using Java:-
String specialCharacters = "[!& *=~}[]"
String specialCharactes1 = "[[[&%*@#(#][[]"
I want the result like this respectively:-
!& *=~}[
[[&%*@#(#][[
That Means I want to remove only one square brackets from both the ends. I saw many solutions but those were related to JavaScript.
This can be done using regex but I am not sure How I can replicate this.
Thanks @Elliott Frisch for the solution. This was just a silly mistake of inverted commas. Your solution worked.
System.out.println(specialCharacters.replaceAll("\\[(.+)\\]", "$1"));
System.out.println(specialCharactes1.replaceAll("\\[(.+)\\]", "$1"));
String specialCharacters = "[!& *=~}[]";
if(null!=specialCharacters)
{
String answer = specialCharacters.substring(specialCharacters.indexOf("
[")+1,specialCharacters.indexOf("]"));
}
This will work if there is only one [ and ] in the input string
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.