String concat() method in java with example:
concat() method is present in the java.lang.String class. concat() method concatenate the given string at a time. This method concatenates the specified string to the end of this string and returns a new string with the new value. This mehtod can be used to concatenate multiple strings one by one. This method can be called multiple times on the string.Syntax:
public String concat(String str)
Return value: String
A string that represents the concatenation of this object's characters followed by the string argument's characters.
In Java 8, a new method join() is introduced. String join() method example to concatenate the given string with a delimiter.
String concat() method example 1:
In this example, we will see how to concat the two strings.
package examples.java.w3schools.string; public class StringConcatExample { public static void main(String[] args) { String value1 = "java"; String value2 = "w3schools"; System.out.println("Value1 and value2 values before concatenating"); System.out.println("Value1 : " + value1); System.out.println("Value2 : " + value2); value2 = value1.concat(value2); System.out.println("Value2 after concatenating value1: " + value2); } }
Here, We can observe that new value2 is appended to value1 at end. Please refer the following output.
Output:
Value1 and value2 values before concatenating Value1 : java Value2 : w3schools Value2 after concatenating value1: javaw3schools
String concat() method example 2:
In example 2, We will see how to concat multiple strings at a time. Previously, we have seen how to add at the end of the string for a single string.
package examples.java.w3schools.string; public class StringConcatExample2 { public static void main(String[] args) { String string1 = "java"; String string2 = "-"; String string3 = "w3schools"; String string4 = " blog"; String finalString = string1.concat(string2).concat(string3).concat(string4); System.out.println("Concatenating multiple strings: " + finalString); } }
Output:
Concatenating multiple strings: java-w3schools blog
String concat() method example 3:
In example 3, We will learn how to concat a string in different way at the beginning of the string.public class StringConcatExample3 { public static void main(String[] args) { String string1 = "java-w3schools"; String finalString = "blogspot.com".concat(string1); System.out.println("Concatenating at begining of existing string: " + finalString); } }
Output:
Concatenating at begining of existing string: blogspot.comjava-w3schools
String concat() method example 4:
If the string2 length is ZERO then original string1 will be returned.String str3 = "hello"; String str4 = ""; str3 = str3.concat(str4); System.out.println("If second string length is 0: " + str3); System.out.println("str3.equals(str4) :: " + str3.equals(str4));
Output:
Concatenating at begining of existing string: blogspot.comjava-w3schools
String concat() method Internal Implementation:
We will see now how concat method is implemented internally or how concat works internally.
below is the internal code.
public String concat(String str) { int olen = str.length(); if (olen == 0) { return this; } if (coder() == str.coder()) { byte[] val = this.value; byte[] oval = str.value; int len = val.length + oval.length; byte[] buf = Arrays.copyOf(val, len); System.arraycopy(oval, 0, buf, val.length, oval.length); return new String(buf, coder); } int len = length(); byte[] buf = StringUTF16.newBytesFor(len + olen); getBytes(buf, 0, UTF16); str.getBytes(buf, len, UTF16); return new String(buf, UTF16); }
Steps:
Here are the steps performed internally for every time concat() method is invoked.
str1.concat(str2);
Remember: In java 8, String uses byte[] to store the characters (not char[] array).
1) Checks the length of str2. If str2 length is 0 then return str1.
2) Checks the coder value of both strings. Gets lengths of both strings.
Creating new byte array with new length.
newLength = str1.length() + str2.length();
Copying the values of str1 into new byte array using Arrays.copyOf(str1, newLength);
Then copying str2 into new byte array by using System.arraycopy(str2, 0, new array, str1.length, str2.length);
Retrun new byte array as string.
3) If coder values different then
Creats a new byte array using StringUTF16.newBytesFor(str1.length + str2.length)
loads the data into new byte array as per UTF16 format.
Retrun new byte array new String(buf, UTF16);
Please post your questions in comments section.
No comments:
Post a Comment
Please do not add any spam links in the comments section.