Java字符数组转字符串

Java字符数组转字符串

在日常开发的过程,我们常见的会用到字符串字符数组,或者字符串数组字符串等等,有时就会因为一个方法的遗忘,导致耽误很多时间或者代码较为冗余。

重点: Sting.join(CharSequence delimiter, CharSequence... elements)

这个方法很容易想起也很容易忘记,下面上案例:
1、字符串数组转字符串

public static void main(String[] args) {
    
    

   String[] strs = new String[]{
    
    "aa", "bb"};
    System.out.println(String.join(", ", strs));
    // 打印: aa, bb    (注意中间有空格)
    
    List<String> list = new ArrayList<>();
    list.add("cd");
    list.add("ef");
    System.out.println(String.join("-", list));
    // 打印: cd-ef
}

2、字符串转字符串数组
这个自然简单,大家都知道的:

String str = "aa,cc";
String[] strs = str.split(",");
List<String> strList = Arrays.asList(strs);

猜你喜欢

转载自blog.csdn.net/qq_50661854/article/details/132204488