-5

这是我的问题

我有这个字符串,我从网络服务中获得价值.....

"2000-07-01 14:29:12","2020-07-01 14:29:12","物业检查","maryam.com","Bakar","Maryam","915ae8fa7cdb44b3-1368080159272 ","05/21/2013 07:28:59","05/09/2013 06:15:59","物业检查","2","","","",""," ","","","","","","","","","","","","","","","","", "","05/09/2013","","","","","","05/09/2013","","假","假","假","错误的”,””,””,””,””,””,"","","","","","假","","","假","","","","","","","", "","","","","","","","","","","05/09/2013","","","","", "","","","","","","05/09/2013","","1.5678106,103.6354891","","",""","","","","","","","","05/09/2013","","","","","",""," ","","","","05/09/2013","","1.5678106,103.6354891","","",""","","","","","","","","05/09/2013","","","","","",""," ","","","","05/09/2013","","1.5678106,103.6354891","","",""

目前我设法使用拆分所有数据

StringTokenizer stringtokenizer = new StringTokenizer(gabung[rline], ",");

但我不希望字符串值从第一个字符串开始直到第 12 个字符串

> "2000-07-01 14:29:12","2020-07-01 14:29:12",,"Property
> Inspection","maryam.com","Bakar","Maryam","915ae8fa7cdb44b3-1368080159272","05/21/2013
> 07:28:59","05/09/2013 06:15:59","Property Inspection","2",

但我想要第 12 个字符串之后的 next 值..

我所做的是使用字符串拆分和拆分使用','和一个计数器..来计算它是否是第12个字符串

而是使用计数器..你们有更好的解决方案吗?我不太了解正则表达式。

4

3 回答 3

1

如果你只想要第 12 个值

使用split:(非常简单,有点低效,但可能还不错——除非我正在编写生产代码,否则我不会打扰其他任何事情)

System.out.println(str.split(",")[12]);

使用indexOf:(更复杂,更高效)

int index = 0;
for (int i = 0; i < 12; i++)
   index = str.indexOf(',', index) + 1;
System.out.println(str.substring(index, str.indexOf(',', index)));

使用正则表达式:(可能比它的价值更复杂)

Pattern pattern = Pattern.compile("^(?:[^,]*,){12}([^,]*)");
Matcher matcher = pattern.matcher(str);
while (matcher.find())
    System.out.println(matcher.group(1));

如果你想要第 12 个值的所有内容

使用indexOf

int index = 0;
for (int i = 0; i < 12; i++)
   index = str.indexOf(',', index) + 1;
System.out.println(str.substring(index));

使用正则表达式:

Pattern pattern = Pattern.compile("^(?:[^,]*,){12}(.*)");
Matcher matcher = pattern.matcher(str);
while (matcher.find())
    System.out.println(matcher.group(1));

测试

查看此页面以获取有关 Java 正则表达式的更多信息。

于 2013-05-24T18:45:44.253 回答
0

如果,每个字符串总是有 94 个分隔符..您可以使用此正则表达式拆分输入

,(?=(?:[^,]*,){82}[^,]*$)

所以

String[] output = inputString.split(aboveRegex);
output[1];//your required value

或者

如果您确定前 12 个字符串,则可以其与此正则表达式匹配

(?:[^,]*,){12}(.*)$//group1 captures your required data
于 2013-05-24T17:37:50.770 回答
0

以下正则表达式将匹配带引号的字符串,后跟可选的逗号

 Pattern p = Pattern.compile("\"[^\"]*\",?");
 Matcher m = p.matcher(INPUT);
 int count = 0;
 while(m.find()) {
     System.out.println(INPUT.substring(m.start(), m.end());
 }
于 2013-05-24T17:32:03.330 回答