如何从字符串中修剪开始和结束双引号?

时间:2022-10-14 00:13:59

I would like to trim a beginning and ending double quote (") from a string.
How can I achieve that in Java? Thanks!

我想从一个字符串修剪一个开头和结尾的双引号(“)。我怎样才能在Java中实现它?谢谢!

12 个解决方案

#1


186  

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

您可以将String#replaceAll()与^ \“| \”$模式一起使用。

E.g.

例如。

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

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

要了解有关正则表达式的更多信息,请访问http://regular-expression.info。

That said, this smells a bit like that you're trying to invent a CSV parser. If so, I'd suggest to look around for existing libraries, such as OpenCSV.

也就是说,这有点像你试图发明一个CSV解析器。如果是这样,我建议四处寻找现有的库,比如OpenCSV。

#2


24  

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

要从字符串中删除第一个字符和最后一个字符,请使用:

myString = myString.substring(1, myString.length()-1);

#3


10  

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

使用Guava,你可以更优雅地写出CharMatcher.is('\“')。trimFrom(mystring);

#4


9  

This is the best way I found, to strip double quotes from the beginning and end of a string.

这是我找到的最好的方法,从字符串的开头和结尾删除双引号。

someString.replace (/(^")|("$)/g, '')

#5


5  

If the double quotes only exist at the beginning and the end, a simple code as this would work perfectly:

如果双引号仅存在于开头和结尾,那么一个简单的代码就可以完美地运行:

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

string = string.replace(“\”“,”“);

#6


3  

First, we check to see if the String is doubled quoted, and if so, remove them. You can skip the conditional if in fact you know it's double quoted.

首先,我们检查String是否加倍引用,如果是,则删除它们。您可以跳过条件,但实际上您知道它是双引号。

if (string.length() >= 2 && string.charAt(0) == '"' && string.charAt(string.length() - 1) == '"')
{
    string = string.substring(1, string.length() - 1);
}

#7


2  

Also with Apache StringUtils.strip():

还有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 both with quoted and unquoted strings as shown in my example. The only downside is, it will not look for strictly matched quotes, only leading and trailing quote characters (ie. no distinction between "partially and "fully" quoted strings).

此方法适用于引用和未引用的字符串,如我的示例所示。唯一的缺点是,它不会寻找严格匹配的引号,只能查找前导和尾随引号字符(即“部分”和“完全”引用的字符串之间没有区别)。

#8


1  

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;
}

#9


0  

find indexes of each double quotes and insert an empty string there.

找到每个双引号的索引并在那里插入一个空字符串。

#10


0  

Matcher m = Pattern.compile("^\"(.*)\"$").matcher(value);
String strUnquoted = value;
if (m.find()) {
    strUnquoted = m.group(1);
}

#11


0  

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

要从Java中的字符串的开头和结尾删除一个或多个双引号,您需要使用基于正则表达式的解决方案:

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

If you need to also remove single quotes:

如果您还需要删除单引号:

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

NOTE: If your string contains " inside, this approach might lead to issues (e.g. "Name": "John" => Name": "John).

注意:如果您的字符串包含“inside”,则此方法可能会导致问题(例如“Name”:“John”=> Name“:”John)。

See a Java demo here:

在这里查看Java演示:

String input_str = "\"'some string'\"";
String result = input_str.replaceAll("^[\"']+|[\"']+$", "");
System.out.println(result); // => some string

#12


0  

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

s.stripPrefix( “\” “)。stripSuffix(” \ “”)

This works regardless of whether the string has or does not have quotes at the start and / or end.

无论字符串在开头和/或结尾是否有引号,这都有效。

Edit: Sorry, Scala only

编辑:对不起,仅限Scala

#1


186  

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

您可以将String#replaceAll()与^ \“| \”$模式一起使用。

E.g.

例如。

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

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

要了解有关正则表达式的更多信息,请访问http://regular-expression.info。

That said, this smells a bit like that you're trying to invent a CSV parser. If so, I'd suggest to look around for existing libraries, such as OpenCSV.

也就是说,这有点像你试图发明一个CSV解析器。如果是这样,我建议四处寻找现有的库,比如OpenCSV。

#2


24  

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

要从字符串中删除第一个字符和最后一个字符,请使用:

myString = myString.substring(1, myString.length()-1);

#3


10  

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

使用Guava,你可以更优雅地写出CharMatcher.is('\“')。trimFrom(mystring);

#4


9  

This is the best way I found, to strip double quotes from the beginning and end of a string.

这是我找到的最好的方法,从字符串的开头和结尾删除双引号。

someString.replace (/(^")|("$)/g, '')

#5


5  

If the double quotes only exist at the beginning and the end, a simple code as this would work perfectly:

如果双引号仅存在于开头和结尾,那么一个简单的代码就可以完美地运行:

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

string = string.replace(“\”“,”“);

#6


3  

First, we check to see if the String is doubled quoted, and if so, remove them. You can skip the conditional if in fact you know it's double quoted.

首先,我们检查String是否加倍引用,如果是,则删除它们。您可以跳过条件,但实际上您知道它是双引号。

if (string.length() >= 2 && string.charAt(0) == '"' && string.charAt(string.length() - 1) == '"')
{
    string = string.substring(1, string.length() - 1);
}

#7


2  

Also with Apache StringUtils.strip():

还有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 both with quoted and unquoted strings as shown in my example. The only downside is, it will not look for strictly matched quotes, only leading and trailing quote characters (ie. no distinction between "partially and "fully" quoted strings).

此方法适用于引用和未引用的字符串,如我的示例所示。唯一的缺点是,它不会寻找严格匹配的引号,只能查找前导和尾随引号字符(即“部分”和“完全”引用的字符串之间没有区别)。

#8


1  

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;
}

#9


0  

find indexes of each double quotes and insert an empty string there.

找到每个双引号的索引并在那里插入一个空字符串。

#10


0  

Matcher m = Pattern.compile("^\"(.*)\"$").matcher(value);
String strUnquoted = value;
if (m.find()) {
    strUnquoted = m.group(1);
}

#11


0  

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

要从Java中的字符串的开头和结尾删除一个或多个双引号,您需要使用基于正则表达式的解决方案:

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

If you need to also remove single quotes:

如果您还需要删除单引号:

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

NOTE: If your string contains " inside, this approach might lead to issues (e.g. "Name": "John" => Name": "John).

注意:如果您的字符串包含“inside”,则此方法可能会导致问题(例如“Name”:“John”=> Name“:”John)。

See a Java demo here:

在这里查看Java演示:

String input_str = "\"'some string'\"";
String result = input_str.replaceAll("^[\"']+|[\"']+$", "");
System.out.println(result); // => some string

#12


0  

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

s.stripPrefix( “\” “)。stripSuffix(” \ “”)

This works regardless of whether the string has or does not have quotes at the start and / or end.

无论字符串在开头和/或结尾是否有引号,这都有效。

Edit: Sorry, Scala only

编辑:对不起,仅限Scala