string.Join
时间: 2024-02-12 18:27:51 浏览: 84
String.join()方法是一个用于拼接字符串的方法。它使用指定的分隔符将多个字符串连接在一起。这个方法在JDK1.8之后才引入。当存在一个元素为null时,它会添加一个"null"字符串。\[1\]
下面是一个示例代码,展示了如何使用String.join()方法:
```java
package com.java265.other;
public class TestStringJoin {
public static void main(String\[\] args) {
String\[\] arrs1 = { "this", "is", "java265.com" };
String\[\] arrs2 = new String\[3\];
arrs2\[0\] = "my";
System.out.println("arrs1:" + String.join(",", arrs1));
System.out.println("arrs2:" + String.join(",", arrs2));
}
}
```
运行以上代码,将输出以下信息:
```
arrs1:this,is,java265.com
arrs2:my,null,null
```
String.join()方法有两种语法形式:
```java
public static String join(CharSequence delimiter, CharSequence... elements)
```
或
```java
public static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements)
```
其中,delimiter参数是每个元素之间要添加的分隔符,elements参数是需要添加分隔符的字符串。该方法返回拼接后的字符串。\[2\]\[3\]
#### 引用[.reference_title]
- *1* *2* *3* [String.join()方法的功能简介说明](https://blog.csdn.net/qq_25073223/article/details/125417038)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文