Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the methods of concatenating the string String in Java

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "what are the methods of concatenating string String in Java". The editor shows you the operation process through an actual case. The method of operation is simple, fast and practical. I hope that this article "what are the methods of concatenating string String in Java" can help you solve the problem.

1. Preface

Java provides a variety of ways to concatenate String strings, but sometimes if we don't pay attention to null strings, we may put null into the result, which is obviously not what we want.

two。 Problem recurrence

If we want to concatenate String arrays, we can simply use the + operator to concatenate, but we may encounter null values.

String [] values = {"https", ": / /", "www.", "wdbyte", ".com", null}; String result = ""; for (String value: values) {result = result + value;}

This splices all elements into the result string, as follows:

Https://www.wdbyte.comnull

However, we have found the problem, and the final null value is also concatenated as a string, which is obviously not what we want.

Similarly, even if we run on Java 8 or later and then use the String.join () static method to concatenate strings, we still get output with a null value.

String [] values = {"https", ": / /", "www.", "wdbyte", ".com", null}; String result = String.join ("", values); / / output: https://www.wdbyte.comnull

Here are some ways to prevent null values from being spliced. My expected output should be:

Https://www.wdbyte.com3. Use the + operator

The addition symbol + can concatenate String strings, so we only need to do null judgment during concatenation to replace the null value with an empty string.

For (String value: values) {result = result + (value = = null? ": value);}

However, we know that String is an immutable object, using the + sign will frequently create a string object, and each time a new string will be created in memory, so using the + symbol to concatenate strings is very performance-intensive.

To facilitate subsequent code demonstrations, we extract a method that can pass in a string and return a non-null string.

Public String nullToString (String value) {return value = = null? "": value;}

So the above code can call this method instead:

For (String value: values) {result = result + nullToString (value);} 4. Use String.concat ()

String.concat () is a method that comes with the String class, and it's convenient to concatenate strings in this way.

For (String value: values) {result = result.concat (getNonNullString (value));}

Because the nullToString () method is called, there is no null value in the result.

5. Use StringBuilder

The StringBuilder class provides many useful and convenient String building methods. The more common method is the append () method, which uses append () to concatenate strings and combines the nullToString () method to avoid null values.

String [] values = {"https", ": / /", "www.", "wdbyte", ".com", null}; StringBuilder result = new StringBuilder (); for (String value: values) {result = result.append (nullToString (value));}

The following results can be obtained:

Https://www.wdbyte.com

6. Use the StringJoiner class (Java 8 +)

The StringJoiner class provides more powerful string concatenation capabilities. You can specify not only delimiters, but also prefixes and suffixes when concatenating. Here we can use its add () method to concatenate strings.

Similarly, the nullToString () method is used to avoid null values.

String [] values = {"https", ": / /", "www.", "wdbyte", ".com", null}; StringJoiner result = new StringJoiner (""); for (String value: values) {result = result.add (nullToString (value));} 7. Use Streams.filter (Java 8 +)

Stream API is a powerful stream operation class introduced by Java 8, which can perform common operations such as filtering, mapping, traversal, grouping, statistics and so on. The filtering operation filter can receive a Predicate function. The Predicate function interface, like the Function (opens new window) interface introduced earlier, is a functional interface. It can accept a generic parameter and the return value is Boolean. Predicate is often used for data filtering.

Therefore, we can define a Predicate to check the string as null, and then pass it to the filter () method of Stream API.

Finally, use the Collectors.joining () method to concatenate the remaining non-null strings.

String [] values = {"https", ": / /", "www.", "wdbyte", ".com", null}; String result = Arrays.stream (values) .filter (Objects::nonNull) .filter (Collectors.joining ()); this is the end of the content about "what are the methods of concatenating the string String in Java". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report