Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove first and last character of a string?

Tags:

java

string

I have worked in a SOAP message to get the LoginToken from a Webservice, and store that LoginToken as a String. U used System.out.println(LoginToken); to print the value. This prints [wdsd34svdf], but I want only wdsd34svdf. How can I remove these square brackets at the start and end of the output?

Example:

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

The output is: [wdsd34svdf].

I want just wdsd34svdf

like image 252
Sampath Kumar Avatar asked Jan 13 '12 05:01

Sampath Kumar


People also ask

How do I remove the first and last characters in a string?

The idea is to use the deleteCharAt() method of StringBuilder class to remove first and the last character of a string. The deleteCharAt() method accepts a parameter as an index of the character you want to remove.

How do I remove the first and last character of a string in Excel?

In Excel 2013 and later versions, there is one more easy way to delete the first and last characters in Excel - the Flash Fill feature. In a cell adjacent to the first cell with the original data, type the desired result omitting the first or last character from the original string, and press Enter.


1 Answers

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 ) ); 
like image 105
gtiwari333 Avatar answered Oct 20 '22 01:10

gtiwari333