How to remove the first and last character of a string?
Solution 1:
You need to find the index of [
and ]
then substring
. (Here [
is always at start and ]
is at end):
String loginToken = "[wdsd34svdf]";
System.out.println( loginToken.substring( 1, loginToken.length() - 1 ) );
Solution 2:
This is generic solution:
str.replaceAll("^.|.$", "")
Solution 3:
You can always use substring
:
String loginToken = getName().toString();
loginToken = loginToken.substring(1, loginToken.length() - 1);