1. 程式人生 > >String.Utils.join(陣列—字串)簡單用法

String.Utils.join(陣列—字串)簡單用法

目錄

將陣列轉換為字串:

需要引入包:import org.apache.commons.lang3.StringUtils;

//將陣列轉換為字串  StringUtils.join(str)

 StringUtils.join(null)              = null

 StringUtils.join([])                = ""

 StringUtils.join([null])            = ""

 StringUtils.join(["a", "b", "c"])   = "abc"

 StringUtils.join([null, "", "a"])   = "a"


// 將陣列用逗號分隔變成字串  StringUtils.join(str, ",")

StringUtils.join(["a", "b", "c"], ",")      = "a,b,c"

之前不知道有這個方法,用了迴圈遍歷的方式來擷取字串的方法來實現的:

List<Cinema> cinemaList=ShiroUtils.getAllMyCinemaList();
String cinemaCode ="";
if(cinemaList.size()>0){
    for (Cinema ca:cinemaList){
        cinemaCode=cinemaCode + ca.getCinemaCode()+",";
    }
}
paramMap.put("cinemaCodes",cinemaCode.substring(0,cinemaCode.length()-1));
StringBuffer cinemaCodes = new StringBuffer();
            for (Cinema cinema : cinemas) {
                cinemaCodes.append(cinema.getCinemaCode()).append(",");
            }
            if(cinemaCodes.length() > 0){
                return cinemaCodes.deleteCharAt(cinemaCodes.length() - 1).toString();
            }else{
                return null;
            }

字串轉換為陣列:

str.split(',');   //以逗號拆分字串

var str = 'ab,c,de';
var a = str.split(','); // [ab, c, de]