| by Arround The Web | No comments

How to Concatenate Strings in Java

While dealing with a bulk amount of data in Java, there can be instances where the developer wants to sort or merge the contained data. For instance, appending a surname with the family name to improve readability. In such situations, concatenating strings in Java is assistive in updating the data or a part of it conveniently.

This blog will elaborate on the approaches to concatenating strings using Java.

What is String Concatenation in Java?

String Concatenation” is the procedure of merging two or more than two strings and forming a new string by appending one string to another one.

How to Concatenate Strings in Java?

The strings in Java can be concatenated using the following approaches:

Approach 1: Concatenate Strings in Java Using “+” Operator

The “+” operator in Java adds two or more values. However, this operator can be utilized to simply concatenate the two specified strings by adding them.

Example

Let’s overview the below-stated example:

String concat1 = "Linux";

String concat2 = "hint";

String result = concat1 + concat2;

System.out.println("The concatenated string is: "+result);

concat1 += concat2;

System.out.println("The concatenated string is: "+concat1);

In the above lines of code:

  • Initialize two string values.
  • In the next step, use the “+” operator in between the string values to concatenate them directly and display the resultant string.
  • Another approach is to use the “+=” operator and display the resultant outcome.

Output

In the given output, it can be seen that the specified strings have been concatenated in both approaches.

Approach 2: Concatenate Strings in Java Using “concat()” Method

The “concat()” method concatenates the string in its parameter to the end of the associated string. This method can be utilized to append the latter string as its parameter to the associated string, thereby concatenating them.

Syntax

String concat(str)

In the above syntax, “str” refers to the string that needs to be concatenated.

Example

Overview the below lines of code:

String concat1 = "Linux";

String concat2 = "hint";

String result = concat1.concat(concat2);

System.out.println("The concatenated string is: "+result);

Apply the following steps as provided in the given code:

  • Likewise, initialize the string values.
  • After that, associate the “concat()” method with the former string such that the string in its parameter is appended to the end of the linked string.
  • Lastly, display the concatenated string on the console.

Output

As observed, the string concatenation is carried out appropriately.

Approach 3: Concatenate Strings in Java Using “String.format()” Method

The “String.format()” method gives the formatted string. This method can be implemented by creating a “String” object and formatting the strings based on that such that the strings become concatenated.

Syntax

String format(str, args object)

In this syntax:

  • str” represents the string format.
  • args object” points to the arguments for the string format.

Example

Go through the below-provided example to understand the stated concept:

String concat1 = new String("Linux");

String concat2 = new String("hint");

String result = String.format("%s%s",concat1, concat2);

System.out.println("The concatenated string is: "+result);

In the above code block:

  • Firstly, create two “String” objects via the “new” keyword and the “String()” constructor, respectively, and allocate the stated string values.
  • In the next step, apply the “format()” method and place the allocated string values as its parameter.
  • Note that “%s” is specified since the values that need to be concatenated are strings.
  • Lastly, display the resultant concatenated string value.

Output

Approach 4: Concatenate Strings in Java Using “String.join()” Method

The “String.join()” method concatenates the provided strings with a delimiter and gives the concatenated string. This method can be implemented to simply join the contained string values in an object based on the specified delimiter.

Syntax

String join(delim, val)

In the above-given syntax:

  • delim” refers to the delimiter that needs to be added with each string.
  • val” corresponds to the “char” value that must be attached with a delimiter.

Example

The below-provided example explains the discussed concept:

String concat1 = new String("Linux");

String concat2 = new String("hint");

String result = String.join("",concat1, concat2);

System.out.println("The concatenated string is: "+result);

In the above code block:

  • Similarly, create two “String” objects and allocate the specified string values.
  • Now, apply the “String.join()” method such that the values as its parameter become concatenated based on the delimiter.
  • Finally, display the concatenated string value.

Output

Approach 5: Concatenate Strings in Java Using “StringBuilder” Class

The Java “StringBuilder” class is used to create an editable succession of characters. The “append()” method accepts/takes arguments of multiple types like StringBuilder, int etc. These combined approaches can be applied to store the string values in objects and then apply concatenation by appending them.

Example

Let’s follow the below-provided code snippet:

StringBuilder concat1 = new StringBuilder("Linux");

StringBuilder concat2 = new StringBuilder("hint");

StringBuilder result = concat1.append(concat2);

System.out.println("The concatenated string is: "+result);

In the above code, simply create two objects of the “StringBuilder” class and concatenate the contained values in the objects via the “append()” method.

Output

The above-generated output signifies that the required task is done successfully.

Conclusion

The strings in Java can be concatenated by using the “+” operator, the “concat()” method, the “String.format()” method, the “String.join()” method, or the “StringBuilder” class. These approaches concatenate the provided strings directly or by placing the string values in an object, respectively. This blog discussed the approaches to concatenate strings in Java.

Share Button

Source: linuxhint.com

Leave a Reply