Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove leading and trailing brackets in a String

In Java, I want to get rid of the leading and trailing brackets in a String.
Given input:

"[ hello, char[]={a,b,c} ... bye ]"

How can I produce the output of

" hello, char[]={a,b,c} ... bye "

only the leading [ and trailing ] is removed... How can I do that in Java?

like image 254
Ronin Avatar asked Dec 04 '25 09:12

Ronin


1 Answers

String i = "[ hello, char[]={a,b,c} ... bye ]";

int indexOfOpenBracket = i.indexOf("[");
int indexOfLastBracket = i.lastIndexOf("]");

System.out.println(i.substring(indexOfOpenBracket+1, indexOfLastBracket));

Prints:

 hello, char[]={a,b,c} ... bye 
like image 188
James Raitsev Avatar answered Dec 05 '25 23:12

James Raitsev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!