Home >>Java String Methods >Java String concat() method
The concat() method for java string combines defined string at the end of this string. Combined string returns. It's like having another string appended.
public String concat(String str)
{
int len1 = str.length();
if (len1 == 0) {
return this;
}
int len = value.length;
char buf[] = Arrays.copyOf(value, len + len1);
str.getChars(buf, len);
return new String(buf, true);
}
public String concat(String anotherString)
anotherString : a separate string to be combined at the end of this string.
combined string
public class ConcatExample
{
public static void main(String args[])
{
String n1="phptpoint";
n1.concat("is immutable");
System.out.println(n1);
n1=n1.concat(" provides full course to learn");
System.out.println(n1);
}}
public class ConcatExample2
{
public static void main(String[] args)
{
String con1 = "Hello";
String con2 = "Phptpoint";
String con3 = "User";
String con4 = con1.concat(con2);
System.out.println(con4);
String con5 = con1.concat(con2).concat(con3);
System.out.println(con5);
}
}
public class ConcatExample3
{
public static void main(String[] args)
{
String num1 = "Hello";
String num2 = "Phptpoint";
String num3 = "User";
String num4 = num1.concat(" ").concat(num2).concat(" ").concat(num3);
System.out.println(num4);
String num5 = num1.concat("…");
System.out.println(num5);
String num6 = num1.concat("*").concat(num2);
System.out.println(num6);
}
}