0

「1 15/8/2012 15:00 palak paneler 2 200 dam aaloo 2 100」のような無数の長い文字列をクライアントから受け取っているので、私の要件は、この文字列を次のようなテーブル形式にすることです。

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

1 に答える 1

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]);
    }
于 2012-08-15T18:44:59.230 に答える