2012-08-15 68 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

相關問題