Reputation: 21895
public static String removeChar(String s, char c) {
StringBuffer r = new StringBuffer( s.length() );
r.setLength( s.length() );
int current = 0;
for (int i = 0; i < s.length(); i ++) {
char cur = s.charAt(i);
if (cur != c) r.setCharAt( current++, cur );
}
return r.toString();
}
I've found the above code here.
Two Questions:
why do we need to do setLength()? without which I am getting java.lang.StringIndexOutOfBoundsException: String index out of range: 0
'ttr' and three junk chars are coming when I run this program with parameters - "teeter" and "e". How to remove the unused whitespaces in the buffer?
Upvotes: 4
Views: 1215
Reputation: 1
You can use:
public static String removeChar(String s, char c) {
StringBuilder r = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char cur = s.charAt(i);
if (cur != c) {
r.append(cur);
}
}
return r.toString();
}
Upvotes: 0
Reputation: 1990
There is a simpler solution:
public static String removeChar(String s, char c) {
return s.replaceAll(Pattern.quote(new StringBuilder().append(c).toString()), "");
}
Upvotes: 0
Reputation: 274888
I'll answer your questions:
1 - why do we need to do setLength()? without which I am getting java.lang.StringIndexOutOfBoundsException: String index out of range: 0
Initially your string buffer has no characters. You need to call setLength
in order to populate your empty string buffer with characters. Null characters, '\0', (or junk characters as you call them) are added to the string buffer so that it reaches the specified length. If you don't, you get a StringIndexOutOfBoundsException
because there are no characters in your string buffer. See Javadocs on StringBuffer#setLength.
So at the end of your method your string buffer has: [t][t][r][\0][\0][\0]
2 - 'ttr' and three junk chars are coming when I run this program with parameters - "teeter" and "e". How to remove the unused whitespaces in the buffer?
You can remove the null characters by calling: r.toString().trim()
or r.substring(0,current)
Upvotes: 2
Reputation: 2365
Ignoring the fact you can use String.replaceAll()...
You should use the append method:
public static String removeChar(final String s, final char c) {
final StringBuffer r = new StringBuffer(s.length());
for (int i = 0; i < s.length(); i++) {
final char cur = s.charAt(i);
if (cur != c) {
r.append(cur);
}
}
return r.toString();
}
Now you won't get the StringIndexOutOfBoundsException.
BTW, in a single threaded environment you should really use StringBuilder.
Upvotes: 0
Reputation: 63734
Every string buffer has a capacity. As long as the length of the character sequence contained in the string buffer does not exceed the capacity, it is not necessary to allocate a new internal buffer array.
API - http://download.oracle.com/javase/6/docs/api/java/lang/StringBuffer.html
Secondly you can just call trim on the string if you want to remove whitespace from the beginning and the end.
But, the other comments suggesting you should use replace/replaceAll are the most pertinent ones. Why rewrite an inbuilt method.
http://download.oracle.com/javase/6/docs/api/java/lang/String.html#replace(char, char)
Upvotes: 0
Reputation: 7070
It seems like you would just want to use the String.replaceAll(OLD, NEW);
Upvotes: 1