java 字符串换行问题

tring test = "fdsfgds,fdesfds,fds,fds,fdsfds,fdsf,fdsfds,fdsf,fds";
我想用“,”来分割他们,然后换行,每行显示3个例如“fdsfgds,fdesfds,fds,”这个样子;需要说明的是,这里的test是从页面传回的值,长度不固定,如果用逗号分开了超过三个就换行,不到三个就不换行
对于 test 可以是任意长度 只要之间用‘,’隔开就行了 。


public class Num {
  
 public Num(){
  
 }
 public String[] init(String str){//转化
  String[] s = new String[str.length()];
  s = str.split(",");
  return s;
 }
 public void show(String[] s){//显示
  int j = 0;
  for(int i =0;i<s.length;i++){
   System.out.print(s[i]+",");
   j++;
   if(j==3){
    j=0;
    System.out.println("");
   }
  }
 }
 
 public static void main(String[] args){
  String test = "fdsfgds,eee,fdesfds,fds,fds,fdsfds,fdsf,fdsfds,fdsf,fds"; 
  Num n = new Num();
  String[] s = n.init(test);
  n.show(s);
  
 }
}
输出结果:
fdsfgds,eee,fdesfds,
fds,fds,fdsfds,
fdsf,fdsfds,fdsf,
fds,

你可能感兴趣的:(java,职场,休闲,字符串换行)