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

How to use StringJoiner

2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge of "how to use StringJoiner". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Basic use

Literally, it is used to concatenate strings, and delimiters are generally needed for concatenation. For example

Hello, world

These three segments are separated by splicing, and if you need to splice, you need to use

StringBuilder sb = new StringBuilder (); sb.append ("hello"); sb.append (","); sb.append ("world")

Now, do not need, just need, easy, use StringJoiner, you can easily complete the splicing, my mother no longer have to worry about my splicing.

Public static void main (String [] args) {StringJoiner stringJoiner = new StringJoiner (","); stringJoiner.add ("hello"); stringJoiner.add ("world"); System.out.println (stringJoiner.toString ());}

Detailed introduction of StringJoiner

Class structure diagram, as shown in the figure

Its parent class is Object, and its member variables have the following contents

String prefix delimiter after prefix concatenation the string delimiter suffix the string suffix value after concatenation emptyValue null value is returned when value is null.

Construction method

Two constructors are provided, one must have a delimiter, the other must have a delimiter, prefix, and suffix

Public method

SetEmptyValue: set null toString: convert to String add: add string merge: merge length: length from another StringJoiner (including prefixes and suffixes)

Streaming API

Public static void main (String [] args) {StringJoiner stringJoiner = new StringJoiner (","). Add ("hello"). Add ("world"); System.out.println (stringJoiner.toString ());}

Output

Hello,world

Suffix splicing

In the example, you need to develop a prefix.

Public static void main (String [] args) {StringJoiner stringJoiner = new StringJoiner (",", "[", "]"); stringJoiner.add ("hello"); stringJoiner.add ("world"); System.out.println (stringJoiner.toString ());}

Output

[hello,world]

Null value processing

Output blank string

Public static void main (String [] args) {StringJoiner stringJoiner = new StringJoiner (","); System.out.println (stringJoiner.toString ());}

Output

[]

Output prefix

Public static void main (String [] args) {StringJoiner stringJoiner = new StringJoiner (",", "[", "]");}

Output

[]

Output specified string

Public static void main (String [] args) {StringJoiner stringJoiner = new StringJoiner (",", "[", "]"); stringJoiner.setEmptyValue ("void"); System.out.println (stringJoiner.toString ());}

Output

Void

String.join ()

Public static void main (String [] args) {String str = String.join (",", "hello", "world"); System.out.println (str);}

Output

This is the end of hello,world 's "how to use StringJoiner". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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