Pregunta

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.

¿Fue útil?

Solución

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.

Otros consejos

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.

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();
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top