Appending String In Java

Appending String In Java | In this blog, we see how to append the string by using the ‘+’ operator. As we all know ‘+’ operator is used for addition operation and also used to concatenate stirngs in the same way as the concat() method does.

Now see the example of appending string in Java:-
Example-1:-
String str = “Book”
char to add = “s”
Appended string = “Books”

Example2:-
String str = “Know”
String to append = “Program”
Appended string = “KnowProgram”

How to Append to a String In Java

Contacting the string using the ‘+’ is as simple as using the concat() method. We just need to use the ‘+’ operator explicitly with the string or character needed to be appended. This can be used to concatenate more than two characters or strings at a time. Following are different ways we can append string:-

  1. Append a character to the string
  2. Append a string to the string

We can append a string to existing string by using the concatenation operator (+) or using Concat() method. The concatenation operator also can be used to append a character to a string. Or, to append a character to the string we can also convert char to string, and then we can append a string to string. Now let us see some examples for it.

Program For Appending String in Java

In the below example we are appending the string to the string using the concatenation operator (+).

public class Main {
   public static void main(String args[]) {
      String str = "Know" + "Program";
      System.out.println(str);
   }
}

Output:-

KnowProgram

The same thing can be done by using the Java string class concat() method. The concat() method of the Java string class is given to concatenate the given string to this string.

public class Main {
   public static void main(String args[]) {
      String str = "Know".concat("Program");
      System.out.println(str);
   }
}

Output:-

KnowProgram

String Appending In Java

In the below program we are appending the character to the end of the string. For this, we can use the concatenation operator (+).

public class Main {
   public static void main(String args[]) {
      String str = "New" + 's';
      System.out.println(str);
   }
}

Output:-

News

We can convert the given character to a string by using the Character.toString() method and then we can append a string to the string by using the above-given ways.

public class Main {
   public static void main(String args[]) {
      String str = "Book" + Character.toString('s');
      System.out.println(str);
   }
}

Output:-

Books

public class Main {
   public static void main(String args[]) {
      String str = "Book".concat(Character.toString('s'));
      System.out.println(str);
   }
}

Output:-

Books

In the below program we are contacting two strings str1 and str2 and storing the same in the third string str3.

public class Main {
   public static void main(String args[]) {
      String str1 = "Hello,";
      String str2 = " Welcome to KnowProgram";
      String str3 = str1 + str2;
      System.out.println("String 1: " + str1);
      System.out.println("String 2: " + str2);
      System.out.println("Appended string: " + str3);
   }
}

Output:-

String 1: Hello,
String 2: Welcome to KnowProgram
Appended string: Hello, Welcome to KnowProgram

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *