Table of contents
1: Problem display
Let's use the "+" method for splicing as an example:
String[] values = {"I ", "want to ", "splicing ", "some ", "charactors ", null};
String result = "";
for (String value : values) {
result = result + value;
}
The result is as follows:
I want to splicing some charactors null
However, we found a problem, the final null
value is also concatenated as a string, which is obviously not what we want.
Also, even if we run on Java 8 or higher, and then use the String.join() static method to concatenate strings, we will get output with null
the value :
String[] values = {"I ", "want to ", "splicing ", "some ", "charactors ", null};
String result = String.join("", values);
// 结果
I want to splicing some charactors null
Two: splicing method
For the convenience of subsequent code demonstrations, we extract a method that can pass in a string and return a non-null string.
public String nullToString(String value) { if (value != null) return value; }
1. Use the "+" operator
The addition symbol "+"null
can splice String strings, then we only need to make a judgment during splicing null
to replace the value with an empty string.
for (String value : values) {
result = result + nullToString(value);
}
However, we know that String is an immutable object, using +
numbers will frequently create string objects, and each time a new string will be created in memory, so the +
performance consumption of using symbols to concatenate strings is very high.
2. Use String.concat()
String.concat()
It is a method that comes with the String class. It is very convenient to use this method to splice strings.
for (String value : values) {
result = result.concat(nullToString(value));
}
3. Use StringBuilder
The StringBuilder class is a relatively practical String processing class newly learned during the learning process.
The StringBuilder class provides many useful and convenient String construction methods. The more commonly used methods are append()
methods, which are used append()
to concatenate strings, and combined with nullToString()
methods to avoid null
values.
String[] values = {"I ", "want to ", "splicing ", "some ", "charactors ", null};
StringBuilder result = new StringBuilder();
for (String value : values) {
result = result.append(nullToString(value));
}
// 结果
I want to splicing some charactors
4. Use StringJoiner
The StringJoiner class is a new method added after Java 8.
The StringJoiner class provides a more powerful string splicing function. Not only can you specify the separator when splicing, but you can also specify the prefix and suffix when splicing. Here we can use its method to splice strings add()
.
The same will use nullToString()
methods to avoid null
values.
String[] values = {"I ", "want to ", "splicing ", "some ", "charactors ", null};
StringJoiner result = new StringJoiner("");
for (String value : values) {
result = result.add(nullToString(value));
}
// 结果
I want to splicing some charactors
5. Use Streams.filter()
Stream API is a powerful streaming operation class introduced by Java 8, which can perform common operations such as filtering, mapping, traversal, grouping, and statistics. The filtering operation filter
can receive a Predicate function. The Predicate function interface is the same as the Function (opens new window) interface. It is a functional interface. It can accept a generic <T>
parameter and return a Boolean type. Predicate is often used for data filtering.
Therefore, we can define a Predicate to check as null
a string, and then pass it to the Stream API filter()
method.
Finally, use Collectors.joining()
the method to splice the remaining non- null
strings.
String[] values = {"I ", "want to ", "splicing ", "some ", "charactors ", null};
String result = Arrays.stream(values).filter(Objects::nonNull).collect(Collectors.joining());