Java8 : StringJoiner

Sachin Joshi
2 min readApr 25, 2022

--

StringJoiner class comes from java.util which is parent of java.lang.Object package.

StringJoiner is used to construct the character sequence (strings) with delimiter provided. Optionally, It can also have the prefix(start) and suffix(end) to be concatenated with the given string.

Though this can be achieved with the StringBuilder class but we need to write additional code to achieve the same.

Constructors:

  1. StringJoiner(CharSequence delimiter)

This will construct a StringJoiner with no characters in it. it will just have a copy of supplied delimiter( no prefix or suffix)

2. StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix)

This will construct a StringJoiner with no characters in it but with copies supplied delimiter, prefix and suffix.

Methods

  1. add(CharSequence newElement)
  2. length()
  3. merge(StringJoiner other)
  4. setEmptyValue(CharSequence emptyValue)
  5. toString()

other than this, there are methods which are inherited from java.lang.Object like clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait.

sample code:

Output:

hit the follow button for more topics on Java, RestAssured, GraphQL, Selenium, JavaScript, JUnit, Jenkins, GitHub Actions, WebUI, SOAP & VBScripts

https://medium.com/@sachinjoshi12687

Also, DM me if you want me to add new topic of your choice urgently.

--

--

Sachin Joshi
Sachin Joshi

Written by Sachin Joshi

follow me for more topics on Java, RestAssured, GraphQL, Selenium, JavaScript, JUnit, Jenkins, GitHub Actions, WebUI, SOAP & VBScripts