Pages

Tuesday, November 16, 2021

Java add char - How to Add Character To String?

1. Overview

In this article, we'll learn how to add char to string in java. Char data type is used to store 1 character in java. Whereas String is used to store a set of characters stored one after another.

Now, we need to add the new char to the string at any position or at a specific position.

You can do this in 3 or more ways as you need.
But, The below methods are used to show the examples to append char to string.

Using the + operator for concat
Using StringBuilder
Using substring()
Java add char - How to Add Character To String?


2. Adding char to string using String and + operator


You can add the char to the string using + operator at the end of the string and start of the string.

In the below example, added a single character and multiple characters at the end of the string.
package com.javaprogramto.chars;

public class AddCharToStringExample {

	public static void main(String[] args) {

		// adding the char at the end of string
		String addingChar = "hello" + 'A';
		
		// adding multiple chars at the end of the string
		String addingChars = "hello" + 'A' + ',' + 'B';
		
		System.out.println("adding single char to the string at the end - "+addingChar);
		System.out.println("adding miltiple chars to the string at the end - "+addingChars);
		
	}
}

Output:
adding single char to the string at the end - helloA
adding miltiple chars to the string at the end - helloA,B
Adding at the string of the string.
package com.javaprogramto.chars;

public class AddCharToStringExample2 {

	public static void main(String[] args) {

		// adding the char at the start of string
		String addingCharAtStart = 'A' + " hello";
		
		// adding multiple chars at the start of the string
		String addingCharsAtStart = 'A' + 'B'+ " hello";
		
		System.out.println("adding single char to the start of the string - "+addingCharAtStart);
		System.out.println("adding miltiple chars to the start of the string  - "+addingCharsAtStart);
	}
}

Output:
adding single char to the start of the string - A hello
adding miltiple chars to the start of the string  - 131 hello
If you observe the output, adding multiple characters at the starting of the string is produced the numeric values instead of the original "AB" characters.

Actually, it took the ascii codes of A and B. A ASCII code is 65 and B ASCII code is 66. Sum 65 and 66 is is 131.

This number is printed on the console along with the string "hello".

3. Appending char to string using StringBuilder


By using StringBuffer.append() method, you can add the chars to the StringBuffer and then convert the StringBuffer to string using toString() method.

Appending the chars to the StringBuffer at the start does not add two ASCII values.
package com.javaprogramto.chars;

public class AddCharToStringExample3 {

	public static void main(String[] args) {

		System.out.println("Single character");

		StringBuffer singleCharBuffer = new StringBuffer();
		singleCharBuffer.append('A').append("hello");
		String singleCharStr = singleCharBuffer.toString();
		System.out.println("start of the string : "+singleCharStr);
		

		StringBuffer singleCharBuffer2 = new StringBuffer();
		singleCharBuffer2.append("hello").append('A');
		String singleCharStr2 = singleCharBuffer2.toString();
		System.out.println("at end of the string : "+singleCharStr2);
		
		System.out.println("Multiple characters");

		StringBuffer singleCharBuffer3 = new StringBuffer();
		singleCharBuffer3.append('A').append('B').append("hello");
		String singleCharStr3 = singleCharBuffer3.toString();
		System.out.println("start of the string : "+singleCharStr3);
		
		StringBuffer singleCharBuffer4 = new StringBuffer();
		singleCharBuffer4.append("hello").append('A').append('B');
		String singleCharStr4 = singleCharBuffer4.toString();
		System.out.println("at end of the string : "+singleCharStr4);
	}
}

Output:
Single character
start of the string : Ahello
at end of the string : helloA
Multiple characters
start of the string : ABhello
at end of the string : helloAB

4. Adding char to string using substring()


You can get the partial string or parts of the string by using substring() method and add char to string at the specific index.

Add char to the middle of the string
package com.javaprogramto.chars;

public class AddCharToStringExample4 {

	public static void main(String[] args) {

		String longString = "very difficult to beat 90% in engineering";

		String addMiddle = longString.substring(0, longString.length() / 2) + ','
				+ longString.substring(longString.length() / 2);
		
		System.out.println("after adding in middle : "+addMiddle);
	}
}

Output:
after adding in middle : very difficult to be,at 90% in engineering

Adding at the beginning and end of the string:
String atStartOfString = 'A' + longString.substring(0);
String atEndOfString = longString.substring(0) + 'A';

System.out.println("At start of string : "+atStartOfString);
System.out.println("At end of string : "+atEndOfString);

Output:
At start of string : Avery difficult to beat 90% in engineering
At end of string : very difficult to beat 90% in engineeringA

5. Conclusion


In this tutorial, we've seen how to add or append the characters to the string in java.

Shown examples are good if you are adding only a single character and multiple characters




No comments:

Post a Comment

Please do not add any spam links in the comments section.