1

I want to do some efficient per-character replacements in a java String, which is the better approach, to work with .toCharArray() or .getBytes() ?

Example code:

// big loop {
    String s = "..###.##...#";
    char[] c = s.toCharArray();
    c[4] = '$';
    c[8] = 'A';
    // etc
// }

If there are some differences between these 2 approaches, or if one is advisable over the other, I would be glad to hear it.

vikingsteve
  • 38,481
  • 23
  • 112
  • 156
  • 1
    http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html – Brian Roach Jan 21 '14 at 09:39
  • Have a look at similar post http://stackoverflow.com/questions/8894258/fastest-way-to-iterate-over-all-the-chars-in-a-string (Fastest way to iterate over all the chars in a String) – Neeraj Krishna Jan 21 '14 at 09:42
  • @BrianRoach am looking for efficiency (array-based operations), not a builder – vikingsteve Jan 21 '14 at 09:45
  • @NeerajKrishna am looking to *modify* the string, not just iterate, so that question is not the same. – vikingsteve Jan 21 '14 at 09:45
  • 1
    @vikingsteve Ignoring that it's unlikely you need such "efficiency" ... a `StringBuilder` is a wrapper around a `char[]` and does exactly what you're describing as wanting to do, providing the methods to do so. It has a constructor you pass your existing `String` to. – Brian Roach Jan 21 '14 at 09:54
  • @BrianRoach ok, I see, but there's still no good reason to use a wrapper when I can use an array. – vikingsteve Jan 21 '14 at 10:10
  • +1 for `StringBuilder`: go for the more convenient API over low-level access. – Louis Wasserman Jan 21 '14 at 18:14
  • Why do i need an api full of operations i dont need? There is nothing wrong with [] as it suits the use case. – vikingsteve Jan 21 '14 at 20:29

3 Answers3

4

toCharArray is better. That way you don't have to deal with encoding. At least not in the normal case (except for characters outside of the Unicode BMP, which are encoded as two surrogate chars). Basically, if you don't have to deal with Chinese text, that's not a problem.

The conversion from a String to a char array is faster than toBytes(): it's basically an array copy, without character encoding trouble. Internally, a String contains a char[], and (at least for Java 7 and newer), not much else.

It's also faster to construct a String back from a char array than from a byte array, because no character encoding is needed.

Thomas Mueller
  • 48,905
  • 14
  • 116
  • 132
1

Use a StringBuilder, which is like a mutable string:

String s = "..###.##...#";
StringBuilder sb = new StringBuilder(s);
sb.setCharAt(4, '$');

There are lots of other methods you can use to change the contents. When you're done:

s = sb.toString();
Bohemian
  • 412,405
  • 93
  • 575
  • 722
1

If you want to compare character values, use toCharArray().

If you want to compare the binary values of the UTF-8 encoding of the characters, use getBytes().

toCharArray is generally faster than toByteArray, but often slower than charAt.

Vinayak Pingale
  • 1,315
  • 8
  • 19