How can I trim the start and end of double quotes from a string?

I would like to trim the beginning and end of the double quote (") from a string.
How can I achieve this in Java? Thank!

+123
java string trim
Apr 09 2018-10-09T00:
source share
16 answers

You can use String#replaceAll() with a pattern ^\"|\"$ for this.

eg.

 string = string.replaceAll("^\"|\"$", ""); 

To learn more about regular expressions, use al ook at http://regular-expression.info .

However, it smells a bit like you are trying to invent a CSV parser. If so, I would suggest looking at existing libraries such as OpenCSV .

+227
Apr 09 '10 at 15:29
source share

To remove the first character and last character from a string, use:

 myString = myString.substring(1, myString.length()-1); 
+28
Apr 09 '10 at 15:31
source share

This is the best way I've found to remove double quotes from the beginning and end of a line.

 someString.replace (/(^")|("$)/g, '') 
+12
Jul 25 '16 at 13:05
source share

Using Guava, you can write more elegantly CharMatcher.is('\"').trimFrom(mystring);

+11
Apr 10 '14 at 9:20
source share

If double quotes exist only at the beginning and at the end, simple code, as this will work fine:

string = string.replace("\"", "");

+11
Aug 22 '14 at 16:57
source share

First, we check that the String is doubled, and if so, delete them. You can skip the conditional if you actually know that it is double quotation marks.

 if (string.length() >= 2 && string.charAt(0) == '"' && string.charAt(string.length() - 1) == '"') { string = string.substring(1, string.length() - 1); } 
+9
Dec 21 '15 at 23:18
source share

Also with Apache StringUtils.strip() :

  StringUtils.strip(null, *) = null StringUtils.strip("", *) = "" StringUtils.strip("abc", null) = "abc" StringUtils.strip(" abc", null) = "abc" StringUtils.strip("abc ", null) = "abc" StringUtils.strip(" abc ", null) = "abc" StringUtils.strip(" abcyx", "xyz") = " abc" 

So,

 final String SchrodingersQuotedString = "may or may not be quoted"; StringUtils.strip(SchrodingersQuotedString, "\""); //quoted no more 

This method works with strings in quotation marks and without quotes, as shown in my example. The only drawback is that it will not look for strictly matching quotation marks, only the start and end characters of quotation marks (ie, there is no difference between "partially "fully" and "fully" quoted strings).

+8
Mar 23 '16 at 14:34
source share

Kotlin

In Kotlin, you can use String.removeSurrounding (delimiter: CharSequence)

for example

 string.removeSurrounding("\"") 

Deletes the specified line of the separator both from the beginning and from the end of this line, if and only if it starts and ends with the separator . Otherwise, returns this string unchanged.

The source code looks like this:

 public fun String.removeSurrounding(delimiter: CharSequence): String = removeSurrounding(delimiter, delimiter) public fun String.removeSurrounding(prefix: CharSequence, suffix: CharSequence): String { if ((length >= prefix.length + suffix.length) && startsWith(prefix) && endsWith(suffix)) { return substring(prefix.length, length - suffix.length) } return this } 
+5
Jul 22 '18 at 22:03
source share

s.stripPrefix("\"").stripSuffix("\"")

This works regardless of whether the string has quotation marks or not at the beginning and / or end.

Edit: Sorry, Scala only

+3
Nov 23 '17 at 2:54 on
source share
 private static String removeQuotesFromStartAndEndOfString(String inputStr) { String result = inputStr; int firstQuote = inputStr.indexOf('\"'); int lastQuote = result.lastIndexOf('\"'); int strLength = inputStr.length(); if (firstQuote == 0 && lastQuote == strLength - 1) { result = result.substring(1, strLength - 1); } return result; } 
+2
Dec 18 '15 at 20:31
source share

The template below, when used with java.util.regex.Matcher , will match any string between double quotes without affecting the occurrence of double quotes inside the string:

 "[^\"][\\p{Print}]*[^\"]" 
+2
Jul 21 '18 at 19:41
source share

To remove one or more double quotes from the beginning and end of a string in Java, you need to use a regex-based solution:

 String result = input_str.replaceAll("^\"+|\"+$", ""); 

If you need to also remove single quotes:

 String result = input_str.replaceAll("^[\"']+|[\"']+$", ""); 

NOTE If your line contains " inside, this approach can lead to problems (for example, "Name": "John" => Name": "John ).

See the Java demo here :

 String input_str = "\"'some string'\""; String result = input_str.replaceAll("^[\"']+|[\"']+$", ""); System.out.println(result); // => some string 
+1
Dec 23 '16 at 9:23
source share

Modifying @brcolow's answer a bit

 if (string != null && string.length() >= 2 && string.startsWith("\"") && string.endsWith("\"") { string = string.substring(1, string.length() - 1); } 
+1
Apr 24 '19 at 17:51
source share

find the indices of each double quotation mark and insert an empty string there.

0
Apr 09 '10 at 15:29
source share
 Matcher m = Pattern.compile("^\"(.*)\"$").matcher(value); String strUnquoted = value; if (m.find()) { strUnquoted = m.group(1); } 
0
Sep 18 '15 at 8:21
source share

Edited: I just realized that I should indicate that this only works if they both exist. Otherwise, the string is not considered a quote. Such a scenario appeared in my case with CSV files.

 org.apache.commons.lang3.StringUtils.unwrap("\"abc\"", "\"") = "abc" org.apache.commons.lang3.StringUtils.unwrap("\"abc", "\"") = "\"abc" org.apache.commons.lang3.StringUtils.unwrap("abc\"", "\"") = "abc\"" 
0
Jan 27 '19 at 10:05
source share



All Articles