This code:
String s = "TEST";
String s2 = s.trim();
s.concat("ING");
System.out.println("S = "+s);
System.out.println("S2 = "+s2);
results in this output:
S = TEST
S2 = TEST
BUILD SUCCESSFUL (total time: 0 seconds)
Why are "TEST" and "ING" not concatenated together?
a String is immutable, meaning you cannot change a String in Java. concat() returns a new, concatenated, string. Show activity on this post.
The concat() method appends (concatenate) a string to the end of another string.
The CONCAT function combines the text from multiple ranges and/or strings, but it doesn't provide delimiter or IgnoreEmpty arguments. CONCAT replaces the CONCATENATE function. However, the CONCATENATE function will stay available for compatibility with earlier versions of Excel.
Java - String concat() Method The method returns a String with the value of the String passed into the method, appended to the end of the String, used to invoke this method.
a String is immutable, meaning you cannot change a String in Java. concat() returns a new, concatenated, string.
String s = "TEST";
String s2 = s.trim();
String s3 = s.concat("ING");
System.out.println("S = "+s);
System.out.println("S2 = "+s2);
System.out.println("S3 = "+s3);
Because String
is immutable - class String
does not contain methods that change the content of the String
object itself. The concat()
method returns a new String
that contains the result of the operation. Instead of this:
s.concat("ING");
Try this:
s = s.concat("ING");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With