Java 如何从字符串中修剪开始和结束的双引号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2608665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I trim beginning and ending double quotes from a string?
提问by ufk
I would like to trim a beginning and ending double quote (") from a string.
How can I achieve that in Java? Thanks!
我想从字符串中修剪开始和结束的双引号 (")。
我怎样才能在 Java 中实现这一点?谢谢!
采纳答案by BalusC
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。
回答by GuruKulki
find indexes of each double quotes and insert an empty string there.
找到每个双引号的索引并在那里插入一个空字符串。
回答by Michael Myers
To remove the first character and last character from the string, use:
要从字符串中删除第一个字符和最后一个字符,请使用:
myString = myString.substring(1, myString.length()-1);
回答by legrass
Using Guava you can write more elegantly CharMatcher.is('\"').trimFrom(mystring);
使用 Guava 你可以写得更优雅 CharMatcher.is('\"').trimFrom(mystring);
回答by sunraincyq
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("\"", "");
回答by Ravikiran
Matcher m = Pattern.compile("^\"(.*)\"$").matcher(value);
String strUnquoted = value;
if (m.find()) {
strUnquoted = m.group(1);
}
回答by m0untp
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;
}
回答by brcolow
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);
}
回答by rath
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 matchedquotes, only leading and trailing quote characters (ie. no distinction between "partially
and "fully"
quoted strings).
此方法适用于带引号和不带引号的字符串,如我的示例所示。唯一的缺点是,它不会查找严格匹配的引号,只会查找前导和尾随引号字符(即,"partially
和带"fully"
引号的字符串之间没有区别)。
回答by Yaniv Levi
This is the best way I found, to strip double quotes from the beginning and end of a string.
这是我发现的最好的方法,从字符串的开头和结尾去掉双引号。
someString.replace (/(^")|("$)/g, '')