Nice looking table with StringBuilder

前端 未结 2 1111
慢半拍i
慢半拍i 2020-12-22 09:35

I know that \"printf\" method can use string formatting.

My question is : Is there a way to create a nice looking table with StringBuilder class?

For exampl

2条回答
  •  有刺的猬
    2020-12-22 09:51

    Ok, so thank you for all the answer who was nice to me.

    I figured out that we could do it by that way :

    public String toString(){
        StringBuilder s = new StringBuilder();
    
        s.append(String.format("%-20s%-20s%-20s%-20s%-20s%-20s%-20s\n","Identifier","Category","Level","Space","Type","Dimension","Limits"));
        s.append(String.format("=============================================================================================================================================\n"));
        for(String id : this.idTable.keySet()) {
            s.append(String.format("%-20s",id));
            s.append(this.idTable.get(id).toString());
            //s.append("Identifier: " +id+" "+this.idTable.get(id).toString()+"\n");
        }
        return s.toString();
    }
    

    Notice, the String.format which do all the work i wanted and didn't know before !

    Regardind to you @Marco13, You did a very good job !!!! Thanks all !

    P.S.: I will aprove the message from @Marco13 , because his implementation is very good also !

提交回复
热议问题