2012-08-15 26 views
0

我从客户端收到了无数长字符串,如“1 15/8/2012 15:00 palak paneer 2 200 dam aaloo 2 100”,所以我的要求是我想要这个字符串表格格式如下:如何在Android中将表格格式的字符串排序

1""""""""""""""""""""""   ''''''''''''''''''''''''''''''''''''''''''''   15/8/2012 15:00 

palak paneer """"""""""""" 2 """"""""""""""""" 200 

dam aaloo '''''''''''''''''''''''''''  2 '''''''''''''''''''''''''''' 100 

等等。

任何帮助,将不胜感激。 为了将整个字符串放入表格格式并且我不知道字符串的长度,我应该使用什么,所以我们不能使用硬编码的值。

我试过string.split函数,我有一个字符串数组,但正如我所说的字符串的长度不固定,所以我们不能做硬编码,所以我应该用什么?

这是我的尝试:

recieved =modifiedSentence.split("~"); 
int length = 0; 
length = recieved.length; 
modifiedSentence = modifiedSentence.substring(length); 
String string =String.format("%+s %+4s",recieved[0],recieved[1]); 
+0

我想,解决方案可以使用字符串操作函数只能找到。第一列只包含字母? – 2012-08-15 16:48:49

回答

0

这个代码可以帮助你,它分裂了received字符串行。

int start=0; 
    String[] rows=new String[10]; 
    int colCount=0; 
    int rowCount=0,i; 
    String str=""; 
    String received="1 15/8/2012 15:00 palak paneer 2 200 dam aaloo 2 100 a a a 3 4"; 
    String splittedStrs[]=received.split("[a-z]"); 
    String header=splittedStrs[0].trim(); 
    received=received.substring(header.length()+1); 
    boolean prev=false; 
    for (i = 0; i < received.length(); i++) { 
     char c=received.charAt(i); 
     if(c==' '){ 
      if(str.matches("\\D+")){       
       prev=true; 
       str=""; 
      } 
      else{ 
       if(prev==true){ 
        colCount++; 
        if(colCount==3){ 
         rows[rowCount]=received.substring(start,i); 
         rowCount++; 
         start=i+1; 
         colCount=0; 
        } 
       } 
       colCount++; 
       if(colCount==3){ 
        rows[rowCount]=received.substring(start,i); 
        rowCount++; 
        start=i+1; 
        colCount=0; 
       } 
       prev=false; 
       str=""; 
      } 
     } 
     else{ 
      str=str.concat(c+""); 
     } 
    } 
    rows[rowCount]=received.substring(start,i); 

    System.out.println("Header ="+header); 
    for (i = 0; i <= rowCount; i++) { 
     System.out.println(rows[i]); 
    } 
+0

感谢回复@Rahmathullah。当我们不知道字符串的长度时它工作吗?我的意思是说,字符串的长度可能会减少或可能增加.... – Rcp 2012-08-16 03:57:22

+0

问题可以使用ArrayList 来解决,其中u可以添加尽可能多的行数。我的意思是,使用字符串行的情况下,使用arraylist – 2012-08-16 04:03:36

+0

谢谢@Rahmathullah M Pulikkal为您的帮助工作..i更小的问题是如何打印palak paneer 2 200 dam aaloo 2 100我的意思是说, nemeric字母是在一条直线上.. – Rcp 2012-08-16 05:03:22

相关问题