$show=/label

Java add char - How to Add Character To String?

SHARE:

A quick guide to how to add char to string in java? and How to append the characters to the String in java?

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




COMMENTS

BLOGGER

About Us

Author: Venkatesh - I love to learn and share the technical stuff.
Name

accumulo,1,ActiveMQ,2,Adsense,1,API,37,ArrayList,18,Arrays,24,Bean Creation,3,Bean Scopes,1,BiConsumer,1,Blogger Tips,1,Books,1,C Programming,1,Collection,8,Collections,37,Collector,1,Command Line,1,Comparator,1,Compile Errors,1,Configurations,7,Constants,1,Control Statements,8,Conversions,6,Core Java,149,Corona India,1,Create,2,CSS,1,Date,3,Date Time API,38,Dictionary,1,Difference,2,Download,1,Eclipse,3,Efficiently,1,Error,1,Errors,1,Exceptions,8,Fast,1,Files,17,Float,1,Font,1,Form,1,Freshers,1,Function,3,Functional Interface,2,Garbage Collector,1,Generics,4,Git,9,Grant,1,Grep,1,HashMap,2,HomeBrew,2,HTML,2,HttpClient,2,Immutable,1,Installation,1,Interview Questions,6,Iterate,2,Jackson API,3,Java,32,Java 10,1,Java 11,6,Java 12,5,Java 13,2,Java 14,2,Java 8,128,Java 8 Difference,2,Java 8 Stream Conversions,4,java 8 Stream Examples,12,Java 9,1,Java Conversions,14,Java Design Patterns,1,Java Files,1,Java Program,3,Java Programs,114,Java Spark,1,java.lang,4,java.util. function,1,JavaScript,1,jQuery,1,Kotlin,11,Kotlin Conversions,6,Kotlin Programs,10,Lambda,2,lang,29,Leap Year,1,live updates,1,LocalDate,1,Logging,1,Mac OS,3,Math,1,Matrix,6,Maven,1,Method References,1,Mockito,1,MongoDB,3,New Features,1,Operations,1,Optional,6,Oracle,5,Oracle 18C,1,Partition,1,Patterns,1,Programs,1,Property,1,Python,2,Quarkus,1,Read,1,Real Time,1,Recursion,2,Remove,2,Rest API,1,Schedules,1,Serialization,1,Servlet,2,Sort,1,Sorting Techniques,8,Spring,2,Spring Boot,23,Spring Email,1,Spring MVC,1,Streams,31,String,61,String Programs,28,String Revese,1,StringBuilder,1,Swing,1,System,1,Tags,1,Threads,11,Tomcat,1,Tomcat 8,1,Troubleshoot,26,Unix,3,Updates,3,util,5,While Loop,1,
ltr
item
JavaProgramTo.com: Java add char - How to Add Character To String?
Java add char - How to Add Character To String?
A quick guide to how to add char to string in java? and How to append the characters to the String in java?
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgcv-jw7dLzLPkJwk-s9km-hfuGBK7Bj9EZv7A0IyQi8HBEa9mzoFeOgj9QBWGoFFxZXx4QZ7SF1ZIiPUoMZuf-AKXoHro9KIohKdOGnGj7IDJ4ROpXVjzxC-FjXLg0Odti8khastnDgXg/w400-h300/Java+add+char+-+How+to+Add+Character+To+String%253F.png
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgcv-jw7dLzLPkJwk-s9km-hfuGBK7Bj9EZv7A0IyQi8HBEa9mzoFeOgj9QBWGoFFxZXx4QZ7SF1ZIiPUoMZuf-AKXoHro9KIohKdOGnGj7IDJ4ROpXVjzxC-FjXLg0Odti8khastnDgXg/s72-w400-c-h300/Java+add+char+-+How+to+Add+Character+To+String%253F.png
JavaProgramTo.com
https://www.javaprogramto.com/2021/11/java-add-char-to-string.html
https://www.javaprogramto.com/
https://www.javaprogramto.com/
https://www.javaprogramto.com/2021/11/java-add-char-to-string.html
true
3124782013468838591
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content