View RSS Feed

Java String

Concatenating Strings

Rate this Entry
by , 04-26-2012 at 06:27 PM (372 Views)
A method is present in String class for concatenation of 2 strings:
Java Code:
string1.concat(string2);
New string will be returned, that is, string1 and string2 will be added in the end. Concat() method could also be used along with string literals:
Java Code:
"My name is ".concat("Zara");
Commonly, strings are concatenated along with +operator:
Java Code:
"Hello," + " world" + "!"
Result would be:
"Hello, world!"
Consider the given example:
Java Code:
public class StringDemo{
   public static void main(String args[]){
      String string1 = "saw I was ";
      System.out.println("Dot " + string1 + "Tod");
   }
}
Following result will be produced:
Dot saw I was Tod

Submit "Concatenating Strings" to Facebook Submit "Concatenating Strings" to Digg Submit "Concatenating Strings" to del.icio.us Submit "Concatenating Strings" to StumbleUpon Submit "Concatenating Strings" to Google

Categories
String

Comments