Friday 1 January 2016

Examples to concatenate strings in Java

In this post, I am going to show how to concatenate two strings in different ways.


1. By using concatenate operator (+)
public class StringConcatenateDemo {
    public static void main(String args[]) {
        String str1 = "PTR";
        String str2 = "Nayan";

        String result = str1 + str2;

        System.out.println("result = " + result);
    }
}

Output
result = PTRNayan

2. By using String concat method

Return a string that represents the concatenation of this object's characters followed by the string argument's characters.
public class StringConcatenateDemo {
    public static void main(String args[]) {
        String str1 = "PTR";
        String str2 = "Nayan";

        String result = str1.concat(str2);

        System.out.println("result = " + result);
    }
}


Output
result = PTRNayan

3. By using String join method
Returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter.

Returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter.

public class StringConcatenateDemo {
    public static void main(String args[]) {
        String str1 = "PTR";
        String str2 = "Nayan";
        String str3 = "ABC";

        String result = String.join(",", str1, str2, str3);

        System.out.println("result = " + result);
    }
}


Output
result = PTR,Nayan,ABC

4. By using String format method
Returns a formatted string using the specified locale, format string, and arguments.

Returns a formatted string using the specified format string and arguments.

public class StringConcatenateDemo {
    public static void main(String args[]) {
        String str1 = "PTR";
        String str2 = "Nayan";
        String str3 = "ABC";

        String result = String.format("%s, %s, %s", str1, str2, str3);

        System.out.println("result = " + result);

    }
}


Output
result = PTR, Nayan, ABC

5. By using StringBuilder
Following link explains the difference between StringBuffer and StringBuilder.

public class StringConcatenateDemo {
    public static void main(String args[]) {
        String firstName = "Hari Krishna";
        String lastName = "Gurram";
        int age = 26;
        String city = "Bangalore";

        StringBuilder builder = new StringBuilder();

        String result = builder.append("(").append("firstName = ")
                .append(firstName).append(", ").append("lastName = ")
                .append(lastName).append(", ").append("age = ").append(age)
                .append(", ").append("city = ").append(city).append(")")
                .toString();

        System.out.println("result : " + result);
    }
}


Output
result : (firstName = Hari Krishna, lastName = Gurram, age = 26, city = Bangalore)

6. By using StringBuffer
public class StringConcatenateDemo {
    public static void main(String args[]) {
        String firstName = "Hari Krishna";
        String lastName = "Gurram";
        int age = 26;
        String city = "Bangalore";

        StringBuffer buffer = new StringBuffer();

        String result = buffer.append("(").append("firstName = ")
                .append(firstName).append(", ").append("lastName = ")
                .append(lastName).append(", ").append("age = ").append(age)
                .append(", ").append("city = ").append(city).append(")")
                .toString();

        System.out.println("result : " + result);
    }
}


Output
result : (firstName = Hari Krishna, lastName = Gurram, age = 26, city = Bangalore)

7. By using StringJoiner
StringJoiner class is added in Java8, used to construct a sequence of characters separated by a delimiter and optionally starting with a supplied prefix and ending with a supplied suffix.
import java.util.StringJoiner;

public class StringConcatenateDemo {
    public static void main(String args[]) {
        String firstName = "Hari Krishna";
        String lastName = "Gurram";
        int age = 26;
        String city = "Bangalore";

        StringJoiner joiner = new StringJoiner("", "(", ")");

        String result = joiner.add("firstName = ").add(firstName).add(", ")
                .add("lastName = ").add(lastName).add(", ").add("age = ")
                .add("" + age).add(", ").add("city = ").add(city).toString();

        System.out.println("result : " + result);
    }
}


Output
result : (firstName = Hari Krishna, lastName = Gurram, age = 26, city = Bangalore)


You may like



                                                 Home

No comments:

Post a Comment