. Advertisement .
..3..
. Advertisement .
..4..
There is no shortage of ways to add char to string in Java. Check them out with examples below.
Add Char To String In Java
With The + Operator
The java.lang.String class supports the plus operator (+)
, which is also the string concatenation operator. You can concatenate two strings into a single string. This is the simplest solution for our goal, able to take any number of arguments and join all of them.
The special thing about the +
operator is that you can provide arguments of other data types as well. It will automatically carry out type casting before the concatenation. A new String object is created every time, which is the resulting value.
This example shows you to use the +
operator to add a char to the end of a Java string:
public class java {
public static void main(String args[]) {
char char1 = '1';
char char2 = '2';
String site1 = "ITTutoria.net ranks " + char1;
String site2 = "Stack Overflow ranks " + char2;
System.out.println(site1);
System.out.println(site2);
}
}
Output:
ITTutoria.net ranks 1
Stack Overflow ranks 2
You can flip the order of the argument if you want to add char to the beginning of the string:
String site1 = char1 + ". ITTutoria";
String site2 = char2 + ". Stack Overflow";
Output:
1. ITTutoria
2. Stack Overflow
With substring()
The substring()
method of the String class returns a String object that contains a substring of the original string. This substring starts and ends at specified indexes.
Syntax:
public String substring(int beginIndex, int endIndex)
If you omit the endIndex parameter, the method extends to the end of the string.
Example:
public class java {
public static void main(String args[]) {
char char1 = '1';
char char2 = '2';
String site1 = "ITTutoria.net ranks ";
String site2 = ". Stack Overflow";
String message1 = site1.substring(0) + char1;
String message2 = site2.substring(0, 0) + char2 + site2.substring(0);
System.out.println(message1);
System.out.println(message2);
}
}
Output:
ITTutoria.net ranks 1
2. Stack Overflow
With StringBuffer.append() and StringBuffer.insert()
The append()
and insert()
methods of the java.lang.StringBuilder
class can help you with this task as well.
As you may already know, Java strings are immutable, meaning we can’t change their content once created. We can only get a new String object when we try to make any modifications, such as with the +
operator or the function substring()
above. Thanks to this nature, we can comfortably share strings across functions without having to worry about data inconsistency.
However, this may also lead to plenty of new objects that need to be discarded during operation. That is why Java offers the StringBuilder and StringBuffer classes, which are both a mutable sequence of characters. You need to stick to StringBuilder because it is more optimized for single-thread use, which occurs more commonly.
The append and insert methods are principal operations of this class. They help you insert or append characters into a string. With insert()
, you can add a char to any point in the string. On the other hand, the character is always added to the end of the string with append()
.
Example:
public class java {
public static void main(String args[]) {
char char1 = '1';
char char2 = '2';
StringBuilder stringBuilder1 = new StringBuilder("ITTutoria.net ranks ");
StringBuilder stringBuilder2 = new StringBuilder("Stack Overflow ranks ");
StringBuilder stringBuilder3 = new StringBuilder(". ITTutoria");
StringBuilder stringBuilder4 = new StringBuilder(". Stack Overflow");
stringBuilder1.append(char1);
stringBuilder2.append(char2);
stringBuilder3.insert(0, char1);
stringBuilder4.insert(0, char2);
System.out.println(stringBuilder1);
System.out.println(stringBuilder2);
System.out.println(stringBuilder3);
System.out.println(stringBuilder4);
}
}
Output:
ITTutoria.net ranks 1
Stack Overflow ranks 2
1. ITTutoria
2. Stack Overflow
Summary
You have many ways to add char to string in Java. You can use the +
operator and the substring()
method of the String class, or use the append()
and insert()
method of the StringBuffer class.
Note: if you want to check whether a string contains a character, check out this guide.
Leave a comment