➤ 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 Remove Special Characters From String | Except for alphanumeric characters remaining other characters are considered special characters. A number or an alphabet is not considered a special character. For example “!@#$%^&*” these characters are termed to be special characters. These characters need not be used everywhere, sometimes they might be unwanted.
Java String Replace Special Characters
Here, we will replace a single special character with an alphabet. To do this we can use the replaceAll() method available in Java. We will print both the actual string and string after the replacement so that the difference can be found easily.
public class Main {
public static void main(String[] args) {
String string = "Kn@w Pr@gram";
System.out.println("Actual String = " + string);
string = string.replaceAll("[@]", "o");
System.out.println("String after replacement = " + string);
}
}
Output:
Actual String = Kn@w Pr@gram
String after replacement = Know Program
In the above program, all “@” special character is replaced with the new character “o”. Hence the string “Kn@w Pr@gram” becomes “Know Program.
How To Remove Special Characters From a String In Java Using Regex
Previously, we replaced all the special characters in a string by using the replaceAll() method. We can do the same by using regular expressions. In the replaceAll() method we use ‘^’ this character selects the other characters except the character mentioned.
public class Main {
public static void main(String[] args) {
String string = "@Know$ ##Pr%ogram*";
System.out.println("Actual String = " + string);
string = string.replaceAll("[^a-zA-Z]", "");
System.out.println("String after replacement = " + string);
}
}
Output:
Actual String = @Know$ ##Pr%ogram*
String after replacement = KnowProgram
Java Replace All Special Characters From String
In this code, we use the if block in the loop to specify the character’s need, except for specified characters all the other characters will be removed. In the if condition we have written the code such that characters other than a-z, A-Z, and 0-9 will be removed from the string. This code also removes spaces.
Java Program to Remove Spaces and Special Characters From String
public class Main {
public static void main(String[] args) {
String string = "Kno$*w;..,98 P7rogr5'a^m?6";
for (int i = 0; i < string.length(); i++) {
if ((string.charAt(i) < 'A' || string.charAt(i) > 'Z') &&
(string.charAt(i) < 'a' || string.charAt(i) > 'z') &&
(string.charAt(i) < '0' || string.charAt(i) > '9')) {
string = string.substring(0, i) + string.substring(i + 1);
i--;
}
}
System.out.print(string);
}
}
Output:
Know98P7rogr5am6
Java Remove Leading Special Characters From String
Now, we remove only the special character in the first place. We can do this by specifying the index. In order to remove the leading first special character, we create a static method removeLeading() which uses the StringBuilder class and used the if loop to remove the special character at the particular index.
public class Main {
public static String removeLeading(String s) {
StringBuilder sb = new StringBuilder(s);
while ( sb.length() > 1 && sb.charAt(0) == '@' ||
sb.charAt(0) == '!' || sb.charAt(0) == '#' ||
sb.charAt(0) == '$' || sb.charAt(0) == '%') {
sb.deleteCharAt(0);
}
return sb.toString();
}
public static void main(String[] args) {
String string = "%Kno$*w;.., Progr'a^m?0";
System.out.println(removeLeading(string));
}
}
Output:-
Kno$*w;.., Progr’a^m?0
Remove Last Special Character From String Java
In order to remove the last special characters from the string, we again create a static method removeLast() which uses the StringBuilder class to manipulate the string by using the if loop.
public class Main {
public static String removeLast(String s) {
StringBuilder sb = new StringBuilder(s);
while (sb.length() > 1 && sb.charAt(sb.length() - 1) == '@' ||
sb.charAt(sb.length() - 1) == '!'||
sb.charAt(sb.length() - 1) == '#' ||
sb.charAt(sb.length() - 1) == '$'||
sb.charAt(sb.length() - 1) == '%') {
sb.setLength(sb.length() - 1);
}
return sb.toString();
}
public static void main(String[] args) {
String s = "%Kno$*w;.., Progr'a^m#";
System.out.println(removeLast(s));
}
}
Output:
%Kno$*w;.., Progr’a^m
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!