Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Saturday, November 20, 2021

Java - How to mix two strings and generate another?

1. Overview

In this tutorial, We'll learn how to mix and concatenate the two or more strings in java.

Strings are used to store the sequence of characters in the order in the memory and they are considered as objects.

Strings are located in java.lang package

If you are new to Strings, please go through the below string methods.



String creation can be done in two ways using new keyword and literals as below.
String s1 = new String("JavaProgramTo.com");
String s2 = "welcome, Java developer";
In the next sections, you will see examples of adding strings using different techniques.

Java - How to mix two strings and generate another?


2. Concatenating Strings using + operator


Adding two or more strings is easily done with the '+' operator

Example 1:
package com.javaprogramto.strings.mix;

public class StringsConcatenateExample1 {

	public static void main(String[] args) {
		
		String s1 = new String("JavaProgramTo.com");
		String s2 = "welcome, Java developer";
		
		String s3 = s1 + s2;
		System.out.println("Mixed string 1 : "+s3);
		
		String s4 = "this is a " + "new string";
		System.out.println("Mixed string 2 : "+s4);	
	}
}

Output:
Mixed string 1 : JavaProgramTo.comwelcome, Java developer
Mixed string 2 : this is a new string
From the output, we can see that '+' operator mixed two strings without losing any info. These strings are stored in the string constant pool.

3. Concatenating Strings using concat() Method


String API comes with a handy utility method concat() is used concatenate strings.

concat() method takes a String object as an argument and adds this string to the current strings. Finally, it returns the concatenated string as result.

Example 2:
package com.javaprogramto.strings.mix;

public class StringsConcatenateExample2 {

	public static void main(String[] args) {

		String s1 = new String("JavaProgramTo.com");
		String s2 = "welcome, Java developer";

		String s3 = s1.concat(s2);
		System.out.println("Mixed string 1 using concat() :: " + s3);

		String s4 = "this is a ".concat("new string");
		System.out.println("Mixed string 2 using concat() : : " + s4);
	}
}
Output:
Mixed string 1 using concat() :: JavaProgramTo.comwelcome, Java developer
Mixed string 2 using concat() : : this is a new stringa

4. Concatenating Strings using StringBuffer and StringBuilder


Java.lang pakakge has two class to work with the string objects. Those are StringBuffer and StringBuilder.

append() method in these two classes takes the string values and appends it to the current value.

Finally, use the function toString() method to convert the builder and buffer objects to strings.

Example 3:
package com.javaprogramto.strings.mix;

public class StringsConcatenateExample3 {

	public static void main(String[] args) {

		String s1 = new String("JavaProgramTo.com");
		String s2 = "welcome, Java developer";

		StringBuffer buffer = new StringBuffer();
		buffer.append(s1);
		buffer.append(s2);
		String s3 = buffer.toString();

		System.out.println("Mixed string 1 using StringBuffer : " + s3);

		StringBuilder builder = new StringBuilder();
		builder.append("this is a ");
		builder.append("new string");
		String s4 = builder.toString();

		System.out.println("Mixed string 2 using StringBuilder : " + s4);
	}
}
Output:
Mixed string 1 using StringBuffer : JavaProgramTo.comwelcome, Java developer
Mixed string 2 using StringBuilder : this is a new string

5. Conclusion


In this article, you've seen the different ways to concatenate strings in java.

GitHub



No comments:

Post a Comment

Please do not add any spam links in the comments section.