How to remove first and last character of a string?

79

I have worked in SOAP message to get LoginToken from Webservice, and store the LoginToken in String and used System.out.println(LoginToken); to print. This prints [wdsd34svdf], but I want only wdsd34svdf so, how to remove square bracket. please any one help me.

Thanks

Example:

String LoginToken=getName().toString();
System.out.println("LoginToken" + LoginToken);

Output: [wdsd34svdf] I want wdsd34svdf

This question is tagged with java string

~ Asked on 2012-01-13 05:05:15

The Best Answer is


179

It's easy, You need to find 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 ) );

~ Answered on 2012-01-13 05:11:37


20

This is generic solution:

str.replaceAll("^.|.$", "")

~ Answered on 2015-08-08 16:35:41


Most Viewed Questions: