0
I need to remove the first and last character from a variable String
, tried to use substring
for this, but is giving the following error:
String index out of range
And I’m not finding a way to do it, I’ve done it so far:
conteudoLote.replace("[", "(").replace("]",")").substring(1,(conteudoLote.length()-1))
Content of the variable:
((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?
And where is the string you want to remove?
– user28595
Your code ran normal for me: https://ideone.com/Y388jZ
– Math
Are you sure
conteudoLote
is not a string with less than two characters?– Victor Stafusa
Not my string
conteudoLote
has more than two characters, I will change my query with the contents of the variable– R.Santos
For me, it works, see here in ideone. Besides, this string of yours doesn’t have any
[
or]
.– Victor Stafusa
If your idea is to produce a JSON, I would use
.replace('(', '[').replace(')',']')
instead of.replace("[", "(").replace("]",")")
.– Victor Stafusa
@Victorstafusa what’s the difference between
replaces
– R.Santos
@R.Santos They are opposites. I exchange parentheses for brackets, while your brackets for brackets. I’m also using replace characters (with single quotes) instead of replace strings (with double quotes), which should perform a little better.
– Victor Stafusa
@Victorstafusa, yes the question of parenthesis exchange for brackets I noticed, is that actually my original string is already with brackets so I do the
replace
so, another thing I noticed and that the error actually occurred when I put thereplace('Suprimento com Defeito e com Resíduo', 'Garantia').replace('Suprimento Bom e com Resíduo', 'Resíduo')
could try adding this to your template code to see if the error occurs to you like this?– R.Santos