String.join()和StringUtils.join()

1. StringUtils.join()

list集合
public static String join(List<?> list,
                          String separator,
                          int startIndex,
                          int endIndex)

Joins the elements of the provided List into a single String containing the provided list of elements.

No delimiter is added before or after the list. Null objects or empty strings within the array are represented by empty strings.

 StringUtils.join(null, *)               = null
 StringUtils.join([], *)                 = ""
 StringUtils.join([null], *)             = ""
 StringUtils.join(["a", "b", "c"], ';')  = "a;b;c"
 StringUtils.join(["a", "b", "c"], null) = "abc"
 StringUtils.join([null, "", "a"], ';')  = ";;a"
 
  • Parameters:

    list - the List of values to join together, may be null

    separator - the separator character to use

    startIndex - the first index to start joining from. It is an error to pass in a start index past the end of the list

    endIndex - the index to stop joining from (exclusive). It is an error to pass in an end index past the end of the list

  • Returns:

    the joined String, null if null list input

  • Since:

    3.8

数组
public static String join(char[] array,
                          char delimiter)

Joins the elements of the provided array into a single String containing the provided list of elements.

No delimiter is added before or after the list. Null objects or empty strings within the array are represented by empty strings.

 StringUtils.join(null, *)               = null
 StringUtils.join([], *)                 = ""
 StringUtils.join([null], *)             = ""
 StringUtils.join([1, 2, 3], ';')  = "1;2;3"
 StringUtils.join([1, 2, 3], null) = "123"
 
  • Parameters:

    array - the array of values to join together, may be null

    delimiter - the separator character to use

  • Returns:

    the joined String, null if null array input

  • Since:

    3.2

2. String.join()

Join array or list of strings
String join(CharSequence delimiter, Iterable<? extends CharSequence> elements)

This method is used to join array of strings or list of strings.

Java program to join list of strings

List<String> strList = Arrays.asList("How", "To", "Do", "In", "Java");
         
String joinedString = String.join(", ", strList);
 
System.out.println(joinedString);
 
Output:
 
How, To, Do, In, Java

Java program to join array of strings

String[] strArray = { "How", "To", "Do", "In", "Java" };
         
String joinedString = String.join(", ", strArray);
 
System.out.println(joinedString);
 
Output:
 
How, To, Do, In, Java
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值