Remove first and last character from a string

0

I need to remove the first and last character of a String variable, I tried to use substring for this, but it is giving the following error:

String index out of range

And I'm not finding a way to do this, I've done it so far:

conteudoLote.replace("[", "(").replace("]",")").substring(1,(conteudoLote.length()-1))

Variable content:

((1,'SCAB17003066B','Suprimento com Defeito e com Resíduo','Lexmark International','60FBX00','Fraco',15),(1,'SCAB160632D9FRET','Vazio','Lexmark International','50FBX00',null,50), (1,'SCAB17003066B','Vazio','Lexmark International','60FBX00',null,null), (1,'SCAB1714435C2','Vazio','Lexmark International','50F0Z00',null,null))

Is there any way to remove the first and last character of a string?

    
asked by anonymous 24.10.2017 / 19:07

3 answers

1

You can make a substring from the second character to the penultimate, like this:

String stringCortada = stringOriginal.substring(1, stringOriginal.length()-1);  
    
24.10.2017 / 19:16
3
public static String removePrimeiroEUltimo(String x) {
    if (x == null) return null;
    if (x.length() <= 2) return "";
    return x.substring(1, x.length() - 1);
}
    
24.10.2017 / 19:15
2

Follows:

String myStr="[wdsd34svdf]";
System.out.println(myStr.substring(1, myStr.length()-1));

Source: link

    
24.10.2017 / 19:15