How to remove the first and last character of a string?

JavaString

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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 - Java

This is generic solution:

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

Solution 3 - Java

You can always use substring:

String loginToken = getName().toString();
loginToken = loginToken.substring(1, loginToken.length() - 1);

Solution 4 - Java

Another solution for this issue is use commons-lang (since version 2.0) StringUtils.substringBetween(String str, String open, String close) method. Main advantage is that it's null safe operation.

StringUtils.substringBetween("[wdsd34svdf]", "[", "]"); // returns wdsd34svdf

Solution 5 - Java

I had a similar scenario, and I thought that something like

str.replaceAll("\[|\]", "");

looked cleaner. Of course, if your token might have brackets in it, that wouldn't work.

Solution 6 - Java

Try this to remove the first and last bracket of string ex.[1,2,3]

String s =str.replaceAll("[", "").replaceAll("]", "");

Exptected result = 1,2,3

Solution 7 - Java

This way you can remove 1 leading "[" and 1 trailing "]" character. If your string happen to not start with "[" or end with "]" it won't remove anything:

str.replaceAll("^\\[|\\]$", "")

Solution 8 - Java

this is perfectly working fine

String str = "[wdsd34svdf]";
//String str1 = str.replace("[","").replace("]", "");
String str1 = str.replaceAll("[^a-zA-Z0-9]", "");
System.out.println(str1);


String strr = "[wdsd(340) svdf]";
String strr1 = str.replaceAll("[^a-zA-Z0-9]", "");
System.out.println(strr1);

Solution 9 - Java

StringUtils's removeStart and removeEnd method help to remove string from start and end of a string.

In this case we could also use combination of this two method

String string = "[wdsd34svdf]";
System.out.println(StringUtils.removeStart(StringUtils.removeEnd(string, "]"), "["));

Solution 10 - Java

SOLUTION 1

def spaceMeOut(str1):

   print(str1[1:len(str1)-1])

str1='Hello'

print(spaceMeOut(str1))

SOLUTION 2

def spaceMeOut(str1):

  res=str1[1:len(str1)-1]

  print('{}'.format(res))

str1='Hello'

print(spaceMeOut(str1))

Solution 11 - Java

In Kotlin

private fun removeLastChar(str: String?): String? {
    return if (str == null || str.isEmpty()) str else str.substring(0, str.length - 1)
}

Solution 12 - Java

This will gives you basic idea

    String str="";
	String str1="";
	Scanner S=new Scanner(System.in);
	System.out.println("Enter the string");
	str=S.nextLine();
	int length=str.length();
	for(int i=0;i<length;i++)
	{
		str1=str.substring(1, length-1);
	}
	System.out.println(str1);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSampath KumarView Question on Stackoverflow
Solution 1 - Javagtiwari333View Answer on Stackoverflow
Solution 2 - Javaphibao37View Answer on Stackoverflow
Solution 3 - JavaTed HoppView Answer on Stackoverflow
Solution 4 - Javamichal.kreuzmanView Answer on Stackoverflow
Solution 5 - JavaV9801View Answer on Stackoverflow
Solution 6 - JavaAbhijit RajmaneView Answer on Stackoverflow
Solution 7 - JavaAndriy FedotovView Answer on Stackoverflow
Solution 8 - JavachidanandView Answer on Stackoverflow
Solution 9 - JavaDebView Answer on Stackoverflow
Solution 10 - Javagoku10View Answer on Stackoverflow
Solution 11 - Javahyung jun yooView Answer on Stackoverflow
Solution 12 - JavaPadmaView Answer on Stackoverflow