21

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 '/' ?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
java baba
  • 2,199
  • 13
  • 33
  • 45
  • You could simply use [`toString().replace("-", "/")`](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replace%28java.lang.CharSequence,%20java.lang.CharSequence%29) – MadProgrammer May 16 '13 at 04:59
  • http://stackoverflow.com/questions/3472663/replace-all-occurences-of-a-string-using-stringbuilder – Habib May 16 '13 at 05:01
  • check this method http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html#setCharAt(int, char) – Nandkumar Tekale May 16 '13 at 05:01
  • -1 The answer is practically in the question, it is hard to imagine you put any thought in to it or did any research or tried anything. – Peter Lawrey May 16 '13 at 05:12

4 Answers4

33

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("-", "/");
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 1
    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 Jan 14 '16 at 10:16
  • @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". – user9999 Nov 01 '20 at 21:23
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("-", "/");
NPKR
  • 5,368
  • 4
  • 31
  • 48
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.

Abubakkar
  • 15,488
  • 8
  • 55
  • 83
  • 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 – user9999 Nov 01 '20 at 21:28
0

Try replacing the characters directly to the string itself.

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

Would output

blah_blah
Rogue
  • 11,105
  • 5
  • 45
  • 71
  • There is no replace(char, char) function for StringBuilder – sanbhat May 16 '13 at 05:00
  • I already tested this, it works fine. sb = StringBuilder, not StringBuffer. – Rogue May 16 '13 at 05:01
  • 1
    @sanbhat Yeah, I thought that to, but Rogue is replaing the characters BEFORE they append to the `StringBuilder` ... `("blah-blah").replace('-', '_')` - A little tricky... – MadProgrammer May 16 '13 at 05:04
  • 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. – Rogue May 16 '13 at 05:06