22

I have a StringBuilder and want to use replace method for a character. code given below

StringBuilder sb = new StringBuilder();
sb.append("01-02-2013");

How can i replace '-' with '/' ?

4

4 Answers 4

35

If don't want to convert the StringBuilder to a String or you need to keep using it/preserve it, then you could do something like this...

for (int index = 0; index < sb.length(); index++) {
    if (sb.charAt(index) == '-') {
        sb.setCharAt(index, '/');
    }
}

If you don't care then you could do something like...

String value = sb.toString().replace("-", "/");
Sign up to request clarification or add additional context in comments.

2 Comments

Please be aware of replace(CharSequence, CharSequence) vs String.replace(char, char), the 2nd form is faster of course as it is a straight forward replacement rather than a Pattern replacement - If you look at the source code of each form you will see what I'm talking about.
@Guido Medina It is not just a replacement. It rewrites all the string to a new array. May be to sustain "immutability". I hoped StringBuilder does it straightforward but looks things are even worse with it. "Convert to String for that" sounds confusing with " A string buffer is like a String, but can be modified." or "use StringBuilder.append instead of String.+ for perfomance".
7

Try this way

StringBuilder sb = new StringBuilder();
            sb.append("01-02-2013");

            sb.replace(sb.indexOf("-"), sb.indexOf("-")+1, "/") ;
            sb.replace(sb.indexOf("-"), sb.indexOf("-")+1, "/") ;
            System.out.println(sb.toString());

Better approch is

sb.toString().replaceAll("-", "/");

2 Comments

indexes are is hard coded
Doesn't replaceAll take regex? I think it should be replace method.
1

You should have searched a little bit about this.

Anyways, when starting to program in java, the best way to know what you can do with java in-built objects is to go to the javadoc of that class and there you will get to know plenty of thing.

In your case, you'll find your answer here : StringBuilder javadoc

Use replace method.

1 Comment

1) it has completely different parameters 2) not just replaces the char value, but capable of changing length, so rebuilding buffer 3)Even if used indirectly via indexOf, IndexOf also takes only String. So
0

Try replacing the characters directly to the string itself.

sb.append(("blah-blah").replace('-', '_'));

Would output

blah_blah

4 Comments

There is no replace(char, char) function for StringBuilder
I already tested this, it works fine. sb = StringBuilder, not StringBuffer.
@sanbhat Yeah, I thought that to, but Rogue is replaing the characters BEFORE they append to the StringBuilder ... ("blah-blah").replace('-', '_') - A little tricky...
Very true. If you wanted to append post-appending (i.e. the whole string), then your own method of sb.toString().replace('-', '/'); would be more ideal. Though, case by case it's all flexibility.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.