【善用新语法,让代码更优美】
jadk1.8为我们提供了String.join()方法
几个使用的例子。让我们在项目灵活的使用它,使代码更加优美
package com.niu.demo;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @description: String.Join的使用
* @author: nxq email: niuxiangqian163@163.com
* @createDate: 2020/12/19 1:58 下午
* @updateUser: nxq email: niuxiangqian163@163.com
* @updateDate: 2020/12/19 1:58 下午
* @updateRemark:
* @version: 1.0
**/
public class Main {
public static void main(String[] args) {
String[] text = {"hello", "word","are","you","ok","?"};
pt(String.join(",", text)); //hello,word,are,you,ok,?
List<String> sList = new ArrayList<>();
sList.add("a");
sList.add("b");
sList.add("c");
pt(String.join("-", sList)); //a-b-c
//在多个字符串对象中间加入连接字符
pt(String.join("\t","I","am","a","student")); //I am a student
pt(String.join("\t \n >>>:","string","a","b","c"));
//string
// >>>:a
// >>>:b
// >>>:c
}
public static void pt(Object o) {
System.out.println(o);
}
}