将数组转化为字符串,并用指定字符分隔(Array&ArrayList)

将数组转换为字符串,并用指定的字符串分隔的简单方法!

  • 普通数组
package com.wr.ArrayUtil;

public class ArrayUtil {
    public static void main(String[] args){
        String[] StrArr={"lily","poly"};
        System.out.println(new ArrayUtil().ArraySplit(StrArr,","));
    }
    /*将数据转换为字符串,并用指定的字符分隔*/
    public String  ArraySplit(String[] SourceStr,String splitChar){
        String resultStr = "";
        for (int i=0;iif(i!=SourceStr.length-1){
                 resultStr +=SourceStr[i]+",";
             }else{
                 resultStr +=SourceStr[i];
             }
        }


        return resultStr;
    }
}
  • ArrayList
package com.wr.ArrayUtil;

import java.util.ArrayList;

public class ArrayUtil {
    public static void main(String[] args){
        ArrayList strArrlist = new ArrayList();
        strArrlist.add("lily1");
        strArrlist.add("poly1");
        strArrlist.add("hanmeimei1");
       // String[] StrArr={"lily","poly","hanmeimei"};
        System.out.println(new ArrayUtil().ArraySplit(strArrlist,","));
    }
    /*将数据转换为字符串,并用指定的字符分隔*/
    public String  ArraySplit( ArrayList strArrlist,String splitChar){
        String resultStr = "";
        for (int i=0;iif(i!=strArrlist.size()-1){
                 resultStr +=strArrlist.get(i).toString()+""+splitChar+"";
             }else{
                 resultStr +=strArrlist.get(i).toString();
             }
        }


        return resultStr;
    }
}

你可能感兴趣的:(java)