简体   繁体   English

用双引号引起来的字符串拆分问题

[英]String enclosed with double quotes splitting issue

I have a String in the below format. 我有以下格式的字符串。

"test","2, High Street, 50100 Eastleigh",",UK"

when i split the above string using below code 当我使用下面的代码分割上面的字符串时

String[] lineArr = line.split("\",", -1)

Actual output is: 实际输出为:

"test
"2, High Street, 50100 Eastleigh

UK"

Expected Output is: 预期输出为:

test
2, High Street, 50100 Eastleigh
,UK

How do i handle this? 我该如何处理?

You can ignore the first and last " , and split on "," , as follows: 您可以忽略第一个和最后一个" ,并分割为"," ,如下所示:

String[] lineArr = line.substring(1,line.length()-1).split("\",\"");

I am sure there are more efficient and elegant solutions, but this should be an easy adaptation of your code with an one-liner. 我敢肯定会有更有效,更优雅的解决方案,但这应该是单线轻松修改您的代码。

split(String match, int limit)

If limit is non-positive then the pattern will be applied as many times as possible and the array can have any length. 如果limit为非正数,则该模式将被尽可能多地应用,并且数组可以具有任何长度。 If limit is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded. 如果limit为零,则该模式将被尽可能多地应用,该数组可以具有任何长度,并且尾随的空字符串将被丢弃。

"test","2, High Street, 50100 Eastleigh","

Here when you apply lineArr.split("\\",",-1) , limit is negative, it will return trailing empty strings as well, 在这里,当您应用lineArr.split("\\",",-1) ,limit为负,它也会返回尾随的空字符串,

test , "2, High Street, 50100 Eastleigh and empty string "" , Hence giving you 3 as length. test"2, High Street, 50100 Eastleighempty string "" ,因此给您3作为长度。

However if you use lineArr.split("\\",",0) or lineArr.split("\\",") no trailing empty strings are returned. 但是,如果您使用lineArr.split("\\",",0)lineArr.split("\\",")不会返回结尾的空字符串。 So you will be getting 2 as length. 因此,您将获得2作为长度。

To eliminate in between empty strings i would recommend you to use lineArr.split("\\","); which is split(String match) version of above split method 为了消除空字符串之间的lineArr.split("\\",");我建议您使用lineArr.split("\\",");它是上述split method split(String match)版本

According to your output you should call lineArr.split("\\",\\""); 根据您的输出,您应该调用lineArr.split("\\",\\""); , this will give your resultant output. ,这将为您提供最终的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用于拆分由|分隔的字符串的正则表达式 当没有用双引号括起来时 - Regex for splitting a string delimited by | when not enclosed on double quotes 在空格上分割字符串,除非使用双引号,但双引号可以附加前面的字符串 - Splitting string on spaces unless in double quotes but double quotes can have a preceding string attached 用逗号分割字符串,并在多个双引号中忽略逗号 - Splitting String by comma and ignore comma in multiple double quotes 在没有被单引号或双引号包围时使用空格分割字符串的正则表达式 - Regex for splitting a string using space when not surrounded by single or double quotes 正则表达式获取双引号中的短语 - Regex for get phrases enclosed in Double Quotes 在 java 中查找用双引号括起来的 substring - Finding a substring that is enclosed withing double quotes in java 硒-查找仅用双引号引起来的文本 - Selenium - Find text only enclosed by double quotes 正则表达式用于在引号括起时删除字符串中的逗号 - Regex for removing comma in a String when it is enclosed by quotes Java中双引号的问题 - Issue with Double Quotes in java 在双引号外使用逗号分隔。 如果是单引号,则忽略双引号 - Splitting on comma outside pair of double quotes . Ignore double quotes if it is single
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM