Search code examples
javastringandroid-layouttabular

How to order a string into table format in Android


I am receiving a countless long string from client like "1 15/8/2012 15:00 palak paneer 2 200 dam aaloo 2 100" and so my requirement is I want this string in a table format like:

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

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

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

and so on.

Any help would be appreciated. What should I use in order to put a whole string into table format and I don't know the length of string, so we can not use hard coded values.

I have tried the string.split function and I have got an array of strings but as I said the length of string is not fixed so we can not do hard coding so what should I have used?

This is my attempt:

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

Solution

  • this code might help you, it splits up the received string to rows.

        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]);
        }