Home  >  Article  >  Java  >  How to use StringJoiner function in Java for string concatenation

How to use StringJoiner function in Java for string concatenation

PHPz
PHPzOriginal
2023-06-26 14:09:101673browse

String concatenation is a very common operation in programming. Java provides a variety of methods for string splicing, one of the more practical methods is to use the StringJoiner function. This article will introduce how to use the StringJoiner function for string splicing.

1. StringJoiner function

The StringJoiner function was introduced in Java 8 and is used to add separators to string sequences. The constructor of the StringJoiner class can pass in three parameters: delimiter, prefix, and suffix, of which the delimiter is required. The StringJoiner class provides the add() method for adding new elements to the string joiner. The toString() method can return the concatenated string.

2. How to use

Let’s learn how to use the StringJoiner function for string splicing through several examples.

1. Basic usage

Let’s first look at the simplest example, adding a space as a separator when splicing two strings "Hello" and "world":

StringJoiner sj = new StringJoiner(" ");
sj.add("Hello");
sj.add("world");
String result = sj.toString();
System.out.println(result);

The output result is:

Hello world

It can be seen that string splicing is very simple, just use the StringJoiner class.

2. Customize suffixes and suffixes

In addition to delimiters, we can also customize suffixes and suffixes. For example, when splicing strings in an array, we may need to add square brackets:

String[] array = {"Hello", "world"};
StringJoiner sj = new StringJoiner(", ", "[", "]");
for (String s : array) {
    sj.add(s);
}
String result = sj.toString();
System.out.println(result);

The output result is:

[Hello, world]

As you can see, we passed in the string when creating the StringJoiner object The three parameters ", ", "[" and "]" represent separator, prefix and suffix respectively. We then loop through each string in the array and add it to the string splicer using the add() method. Finally, call the toString() method to get the spliced ​​string.

  1. Processing when null values ​​are passed in

Since the parameters passed in the StringJoiner constructor in Java8 are only meaningful with non-null values, in add A NullPointerException will be thrown when a null value is added to the () method. The way to solve this problem is to add a default value in the constructor.

StringJoiner sj = new StringJoiner(", ", "[", "]");
sj.setEmptyValue("No values added yet");
String[] array = {}; // empty array
for (String s : array) {
    sj.add(s);
}
System.out.println(sj.toString());

In the above code, we passed in an empty string array. Since the array is empty, we just need to set a default value, indicating that no value has been added yet. If not set, the output will be an empty string. If some values ​​are added, the output result is:

No values added yet
  1. Other usage

In addition to the above usage, StringJoiner has some other methods that can be used, such as:

  • setEmptyValue(): Set the default value used when there is no element. If not set, the default value is "".
  • add(CharSequence csq): Add a character sequence.
  • merge(StringJoiner other): Add elements from other StringJoiner objects to the current object.
  • length(): Returns the number of characters in the current StringJoiner, excluding suffixes and delimiters.
  • toString(): Returns the string spliced ​​by StringJoiner.

3. Summary

The StringJoiner function is a newly introduced string splicer in Java 8, which can easily splice multiple strings. Through the introduction of this article, we can see that the use of the StringJoiner function is very simple. You only need to create a StringJoiner object, set the separator, suffix and other parameters, and then use the add() method to add the strings that need to be spliced ​​to the splicer. Just hit it. At the same time, it should be noted that when the added value is a null value, a default value needs to be set, otherwise the program may throw a null pointer exception.

The above is the detailed content of How to use StringJoiner function in Java for string concatenation. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn