2012-06-19 40 views
0

我想分割从数据库结果集行生成的以下字符串。Java字符串split()分隔符为零长度值

样品行:

A)(74 “并有”, “是一个汽车”, “中”, “车库的人”,T)

B)(17,account3, ,,,吨)

我想要的输出如下(管|分离分割元素):

A)74 |并在那里是汽车|在|中车库人| t

B)17 | account3 | | | | t

我想在我的应用程序中将零长度值存储为null。这是我的源代码:

public void refreshSearchTable (ArrayList<String> newData){ 
     int noOfRows = newData.size(); 
     int noOfColumns = gui.getTableSearchResultHeader().length + 1; 
     Object[][] tempList = new Object[noOfRows][noOfColumns]; 
     String rowResult = ""; 
     String delims = "[,()\"]+"; 
     String [] tokens; 
     Object [] elements = newData.toArray(); 

     int j = 0; 
     for (int i = 0; i < noOfRows; i++){ 
      rowResult = elements[i].toString(); 
      System.out.println("rowresult: " + rowResult); 
      tokens = rowResult.split(delims, -1); 
      for (String t : tokens){ 

       System.out.println("j: " + j + "t: " + t); 

       if (j == 1){ ... } 
       else if (j==2){ ... } 
           ... 

       j++; 
      } 
      j=0; 
     } 
    } 

“[()\”]为非零长度值+”工作得很好,我能够根据索引值j拿起右边的子串。我已经读过,通过一个负值分割(,)将不会丢弃非零长度值...但它确实。我在做什么错?

回答

3

也许你可以用.split("[()\"]*,{1}[()\"]*")尝试;

[,()\"]+之前的表达式用户将用作分隔符多于一个逗号或任何其他分隔符通知。例如,,将被考虑为一个,[],[]也是如此。上面的表达式注释,预计零个或多个分隔符后面只有一个逗号,并且可以跟零个或多个分隔符。

+0

这个效果很好!唯一的问题是在要拆分的字符串的开始和结尾处的开头和结尾括号出现在第一个和最后一个拆分元素中。 'rowresult:(74,“and there”,“is a car”,“in the”,“garage man”,t) j:0t:(74 j:1t:and there j:2t:is a汽车 j:3t:在 j:4t:车库人 j:5t:t)' – greatkalu

+0

不能手动删除此括号吗?例如: if(str.startsWith(“(”))str = str.substring(1);和 if(str.endsWith(“)”))str = str.substring(0,str.length ()-1);'执行拆分之前? –

+0

也许你可以使用相同的策略使用替换,这将有助于一点...例如'字符串str =“(74,\”和那里\“,\”是一个汽车\“,\”在\ “,”车库男子“,t)”; if(str.startsWith(“(”))str = str.substring(1); if(str.endsWith(“)”))str = str.substring(0,str.length() - 1); String result = str.replaceAll(“[()\”] *,{1} [()\“] *”,“|”);' –

0

为什么你没有使用String.replace or replaceall,它会工作作为当前的程序,但如果你需要跳过字符串,你应该的空白部分,首先

  • 创建字符串构建
  • 追加输入数组列表中的所有数据(条件是数据长度== 0或数据为空只是跳过它)
  • last append your symbol |
+0

感谢。我会尽快研究它并发布。 – greatkalu

0

谢谢Spaeth。这是他们需要的方式。对不起,我在原来的问题中添加了管道。我只是键入它们来分隔元素。为了维护结果集中的列,我需要根据由逗号定义的特定索引来设置元素。

public void refreshSearchTable (ArrayList<String> newData){ 
     int noOfRows = newData.size(); 
     int noOfColumns = gui.getTableSearchResultHeader().length + 2; 
     Object[][] tempList = new Object[noOfRows][noOfColumns]; 
     String rowResult = ""; 
     String delims = "[()\"]*,{1}[()\"]*"; 
     String [] tokens; 
     Object [] elements = newData.toArray(); 

     int j = 0; 
     for (int i = 0; i < noOfRows; i++){ 
      rowResult = elements[i].toString(); 
      if (rowResult.startsWith("(")){ 
       rowResult = rowResult.substring(1); 
      } 
      if (rowResult.endsWith(")")){ 
       rowResult = rowResult.substring(0, rowResult.length()-1); 
      } 
      System.out.println("rowresult: " + rowResult); 
      tokens = rowResult.split(delims); 
      for (String t : tokens){ 

       System.out.println("j: " + j + "t: " + t); 

       j++; 
      } 
      j=0; 
     } 

输出:

rowresult: 74,"and there","is a car","in the","garage man",t 
j: 0t: 74 
j: 1t: and there 
j: 2t: is a car 
j: 3t: in the 
j: 4t: garage man 
j: 5t: t