➤ Sum of digits in a String
➤ Count No of Vowels String
➤ String Pattern Programs in Java
➤ Take String Input In Java
➤ Take Multiple String Input in Java
➤ How To Reverse a String In Java
➤ Remove Special Characters
➤ String – Remove Character
➤ String Palindrome In Java
➤ Sort String In Java
➤ How to Compare Strings In Java
➤ Second Occurrence of Character
➤ Replace nth Occurrence String
➤ Last Occurrence of Character
➤ Uppercase & Lowercase
➤ Java Check If Char is Uppercase
➤ Check If String is Uppercase
➤ Swap Characters in String Java
➤ Java String indexOf() Method
➤ How to Replace Dot in Java?
➤ How to Find Length of String
➤ Substring Method In Java
➤ Split Method In Java
Java Set Character in String | In this blog, we will learn how to add a particular string to a string at a particular position. For instance, see the below example:-
1) String = “java”
Character to add to the end of string = ‘c’
Resultant string = “javac”
2) String = “now”
Character to add to the start of string = ‘k’
Resultant string = “know”
3) String = “clan”
Character to add = ‘e’
Position = 2
Resultant String = “clean”
Java Set Character in String at End
In this example, we will set the character at the end of the string. To add a character at the end of the string we can use the concatenation operator (+). The concatenation operator can be used to add two strings. For this, we have to convert the character to a string. Let us see it through an example:-
public class Main {
public static void main(String[] args) {
String str = "HER";
char ch = 'O';
System.out.println("Before: " + str);
str = str + Character.toString(ch);
System.out.println("After: " + str);
String str1 = "pin";
char ch1 = 'e';
System.out.println("Before: " + str1);
str1 = str1 + Character.toString(ch1);
System.out.println("After: " + str1);
String str2 = "hone";
char ch2 = 'y';
System.out.println("Before: " + str2);
str2 = str2 + Character.toString(ch2);
System.out.println("After: " + str2);
}
}
Output:-
Before: HER
After: HERO
Before: pin
After: pine
Before: hone
After: honey
The Character.toString() method is present in Java.Lang package, which takes character ‘c’ as a parameter and returns the converted string. The method details of the Character.toString() method is as follows:- String java.lang.Character.toString(char c).
Java Set Character in String at Beginning
This is also very similar to the previous example. In the previous program, we were adding a character at the end of the string but now we will add a character at the beginning of the string.
public class Main {
public static void main(String[] args) {
String str = "range";
char ch = 'o';
System.out.println("Before: " + str);
str = Character.toString(ch) + str;
System.out.println("After: " + str);
String str1 = "other";
char ch1 = 'm';
System.out.println("Before: " + str1);
str1 = Character.toString(ch1) + str1;
System.out.println("After: " + str1);
String str2 = "issue";
char ch2 = 't';
System.out.println("Before: " + str2);
str2 = Character.toString(ch2) + str2;
System.out.println("After: " + str2);
String str3 = "our";
char ch3 = 'p';
System.out.println("Before: " + str3);
str3 = Character.toString(ch3) + str3;
System.out.println("After: " + str3);
}
}
Output:-
Before: range
After: orange
Before: other
After: mother
Before: issue
After: tissue
Before: our
After: pour
Java Set Character in String at Between
In this code, we have used the substring() method to place the character at the required position. The substring() method is used to retrieve the substring in the particular index or from the particular index. The method details of the substring method are as follows:- public string substring(int position).
public class Main {
public static void main(String[] args) {
String str = "got";
char ch = 'a';
int position = 2;
System.out.println("Before: " + str);
str = str.substring(0, position) + Character.toString(ch) +
str.substring(position, str.length());
System.out.println("After: " + str);
String str1 = "cat";
char ch1 = 'h';
int position1 = 1;
System.out.println("Before: " + str1);
str1 = str1.substring(0, position1) + Character.toString(ch1) +
str1.substring(position1, str1.length());
System.out.println("After: " + str1);
}
}
Output:-
Before: got
After: goat
Before: cat
After: chat
Change Character In String Java
Instead of adding/setting a character in the string sometimes we need to change the character in the string. There are two possibilities while changing a character in string Java:-
- Change one character at a specific position/index.
- Replace all the occurrences of a particular character with another character in the given string.
Example of changing one character at a specific position/index:-
String = “Stronger”
Position to change the character = 3
New character to replace = ‘a’
Resultant string = “Stranger”
Example of replacing all the occurrences of a particular character with another character in the given string:-
String = “Java programming language”
Character to be replaced = ‘a’
New character = ‘X’
Resultant string = “JXvX ProgrXmming LXnguXge”
Java String Replace One Character at Given index
In this program, we will replace a particular character at the given index with another character. For this, we have to use the substring() method.
public class Main {
public static void main(String[] args) {
String str = "Stronger";
char ch = 'a';
int position = 3;
System.out.println("Before: " + str);
str = str.substring(0, position) + Character.toString(ch) +
str.substring(position+1, str.length());
System.out.println("After: " + str);
}
}
Output:-
Before: Stronger
After: Stranger
Java String Change Char All Occurrence
Now, we will see an example to replace all occurrences of a particular character in the given string. For this, we can use replace() or replaceAll() method of the Java String class.
public class Main {
public static void main(String[] args) {
String str = "Java Programming Language";
char oldChar = 'a';
char newChar = 'X';
System.out.println("Before: " + str);
str = str.replace(oldChar, newChar);
System.out.println("After: " + str);
}
}
Output:-
Before: Java Programming Language
After: JXvX ProgrXmming LXnguXge
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!