
How to remove/replace character in a String in Java?
The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class.
You are at right place if you have below questions:
- How to Remove Special Characters from String in Java
- Java Remove Character from String
- How to remove single character from a String
- Remove or Replace part of a String in Java
Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared.
Create class CrunchifyStringReplaceDelete.java
package crunchify.com.tutorials;
/**
* @author Crunchify.com
* In Java How to Replace/Remove Characters from String?
*/
public class CrunchifyStringReplaceDelete {
public static void main(String[] args) {
System.out.println("Output for replaceAllChar(): " + repalceAllChar("Crunchify.com - Web Development", "e", "*"));
System.out.println("Output for deleteAllNonDigit(): " + deleteAllNonDigit("#21sadfs23$&%^(!9@!"));
System.out.println("Output for replaceCharAt(): " + replaceCharAt("eBay Google Paypal", 10, '$'));
System.out.println("Output for removeChar(): " + removeChar("eBay Google Paypal", 'a'));
System.out.println("Output for removeCharAt(): " + removeCharAt("eBay Google Paypal", 5));
}
private static String removeCharAt(String s, int i) {
StringBuffer crunchifyBuffer = new StringBuffer(s.length() - 1);
// AbstractStringBuilder Appends the specified string to this character sequence.
// The characters of the String argument are appended, in order, increasing the length of this sequence by the length of the argument.
// If str is null, then the four characters "null" are appended.
crunchifyBuffer.append(s.substring(0, i)).append(s.substring(i + 1));
return crunchifyBuffer.toString();
}
private static String removeChar(String s, char c) {
StringBuffer buf = new StringBuffer(s.length());
buf.setLength(s.length());
int current = 0;
for (int i = 0; i < s.length(); i++) {
// charAt() Returns the char value at the specified index. An index ranges from 0 to length() - 1.
// The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing.
char cur = s.charAt(i);
if (cur != c) buf.setCharAt(current++, cur);
}
return buf.toString();
}
private static String replaceCharAt(String s, int i, char c) {
StringBuffer buf = new StringBuffer(s);
buf.setCharAt(i, c);
return buf.toString();
}
private static String deleteAllNonDigit(String s) {
// Replaces each substring of this string that matches the given regular expression with the given replacement.
// An invocation of this method of the form str.replaceAll(regex, repl) yields exactly the same result as the expression
String temp = s.replaceAll("\\D", "");
return temp;
}
public static String repalceAllChar(String s, String f, String r) {
// replace() Replaces each substring of this string that matches the literal target
// sequence with the specified literal replacement sequence.
String temp = s.replace(f, r);
return temp;
}
}
Just run above program as Java Application and you should see result like this.
Eclipse Console Result:
Output for replaceAllChar(): Crunchify.com - W*b D*v*lopm*nt Output for deleteAllNonDigit(): 21239 Output for replaceCharAt(): eBay Googl$ Paypal Output for removeChar(): eBy Google Pypl Output for removeCharAt(): eBay oogle Paypal Process finished with exit code 0
