Skip to content

Commit ca34b42

Browse files
committed
feat:[Java 中拼接 String 的 N 种方式](https://www.wdbyte.com/java/string-concat.html)
1 parent 01bc5f2 commit ca34b42

File tree

4 files changed

+114
-1
lines changed

4 files changed

+114
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ Java 版本任你发,我用 Java 8 。但是多学点这种装x技巧总没错
141141
-
142142

143143
## ⏳ Java 开发
144-
144+
- [Java 中拼接 String 的 N 种方式](https://www.wdbyte.com/java/string-concat.html)
145145
- [字符图案,我用字符画个冰墩墩](https://www.wdbyte.com/java/char-image.html)
146146
- [Java 中 RMI 的使用](https://www.wdbyte.com/2021/05/java/java-rmi/)
147147
- [如何使用 Github Actions 自动抓取每日必应壁纸?](https://www.wdbyte.com/2021/03/bing-wallpaper-github-action/)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## core-java-io
2+
当前模块包含 String 相关代码
3+
4+
### 相关文章
5+
6+
- [Java 中拼接 String 的 N 种方式](https://www.wdbyte.com/java/string-concat.html)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>core-java-modules</artifactId>
7+
<groupId>com.wdbyte.core-java-modules</groupId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>core-java-string</artifactId>
13+
14+
<properties>
15+
<maven.compiler.source>17</maven.compiler.source>
16+
<maven.compiler.target>17</maven.compiler.target>
17+
</properties>
18+
19+
</project>
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.wdbyte.string;
2+
3+
import java.util.Arrays;
4+
import java.util.Objects;
5+
import java.util.StringJoiner;
6+
import java.util.stream.Collectors;
7+
8+
/**
9+
* @author niulang
10+
* @date 2022/02/16
11+
*/
12+
public class StringConcat {
13+
14+
public static void main(String[] args) {
15+
System.out.println(concat());
16+
System.out.println(concat2());
17+
System.out.println(concat3());
18+
System.out.println(concat4());
19+
System.out.println(concat5());
20+
System.out.println(concat6());
21+
System.out.println(concat7());
22+
}
23+
24+
public static String concat() {
25+
String[] values = {"https", "://", "www.", "wdbyte", ".com", null};
26+
String result = "";
27+
28+
for (String value : values) {
29+
result = result + value;
30+
}
31+
return result;
32+
}
33+
34+
public static String concat2() {
35+
String[] values = {"https", "://", "www.", "wdbyte", ".com", null};
36+
String result = String.join("", values);
37+
return result;
38+
}
39+
40+
public static String concat3() {
41+
String[] values = {"https", "://", "www.", "wdbyte", ".com", null};
42+
String result = "";
43+
for (String value : values) {
44+
result = result + nullToString(value);
45+
}
46+
return result;
47+
}
48+
49+
public static String concat4() {
50+
String[] values = {"https", "://", "www.", "wdbyte", ".com", null};
51+
String result = "";
52+
for (String value : values) {
53+
result = result.concat(nullToString(value));
54+
}
55+
return result;
56+
}
57+
58+
public static String concat5() {
59+
String[] values = {"https", "://", "www.", "wdbyte", ".com", null};
60+
StringBuilder result = new StringBuilder();
61+
for (String value : values) {
62+
result = result.append(nullToString(value));
63+
}
64+
return result.toString();
65+
}
66+
67+
public static String concat6() {
68+
String[] values = {"https", "://", "www.", "wdbyte", ".com", null};
69+
StringJoiner result = new StringJoiner("");
70+
for (String value : values) {
71+
result = result.add(nullToString(value));
72+
}
73+
return result.toString();
74+
}
75+
76+
public static String concat7() {
77+
String[] values = {"https", "://", "www.", "wdbyte", ".com", null};
78+
String result = Arrays.stream(values)
79+
.filter(Objects::nonNull)
80+
.collect(Collectors.joining());
81+
return result;
82+
}
83+
84+
public static String nullToString(String value) {
85+
return value == null ? "" : value;
86+
}
87+
88+
}

0 commit comments

Comments
 (0)