How to convert java string array to string with delimiter

Using String.join method (from java 8)

Using simple for-loop expression

 

How to convert an int to a formatted string in java

Using java.util.formatter

See the Formatter docs for other modifiers.

How to repeat string ‘n’ times in java?

Suppose you have the string ‘qwe’ and you need to repeat string ‘n’ times. Here’s how to do it:

Using java 8 stream api

Using String.format

Let us examine this example in more detail. Primitive types ( char[] , in this case) are instantiated with nulls “number of times”, then a String is created from the char[] , and the nulls are replaced() with the original string str.

Using StringBuilder and loop

And last but not least, you can use StringBuilder and loop

Using third-party libraries

You could use Apache commons-lang (which has an impressive collection of handy string utilities):

 

How to compare strings in Java?

Test strings equality

==  tests object references, .equals()  tests the string values.

Sometimes it looks as if == compares values, because Java does some behind-the-scenes stuff to make sure identical in-line strings are actually the same object.

For example:

Correct null handling

== handles null strings fine, but calling .equals() from a null string will cause an exception: