What is the best way to replace a character in a given String position?

Asked

Viewed 8,755 times

5

I need to replace a character at a certain String position().

Example - Swap character[21] with 'X':

String Original = 01078989469150999134B B 2116456

Modified String = 01078989469150999134B X 2116456

I’m using the following code:

StringBuffer sb = new StringBuffer('01078989469150999134BB2116456');
sb.setCharAt(21, 'X');
String novaStr = sb.toString();

However, from what I read and understood, it is expensive to use Stringbuffer(), especially in this process, which will be used several times (barcode reading).

The question is - Is this method effective? Is there a better and more efficient way?

3 answers

5


There are three classes options to handle Strings: String, Stringbuffer and Stringbuilder.

Is used:

  • String: when you don’t want to modify the text too much;
  • Stringbuilder when you want to make numerous modifications to the text.
  • Stringbuffer when you want to make numerous changes to the text and for the variable to be thread-safe.

Therefore, the most efficient class to make modifications is Stringbuilder, however you should not choose it if you are using the same variable for different Threads at the same time, which is the case of choosing Stringbuffer.

If you were running this operation only once in the middle of your code, efficiency is something that matters little. So any of the choices wouldn’t change much.

As you said you are doing the operation numerous times, use Stringbuilder. The syntax is the same, see the example:

StringBuilder s = new StringBuilder("01078989469150999134BB2116456");
s.setCharAt(21, 'X');
System.out.println(s.toString());

3

An alternative would be to use the StringBuilder instead of StringBuffer, because the second uses synchronized methods and the first does not, and as we know, the synchronization of methods affects the performance, since only one thread accesses each method at a time. What the code would look like:

StringBuilder sb = new StringBuilder("01078989469150999134BB2116456");
sb.setCharAt(21, 'X');
String novaString = sb.toString();
System.out.println(novaString);

Another alternative would be not to use StringBuilder and neither StringBuffer, I just won’t be able to tell you about the performance issue:

String codigo = "01078989469150999134BB2116456";
char[] codigoChar = codigo.toCharArray();
codigoChar[21] = 'X';
codigo = String.valueOf(codigoChar);
System.out.println(codigo);
  • 3

    The class AbstractStringBuilder which is extended so much by StringBuffer as per StringBuilder internally uses an array of char. So the efficiency is pretty much the same, except for the so-called method and the limit check. The only efficiency problem that there may be in some cases is whether routine convert String from and to StringBuilder at all times, that is, a bad implementation would create a StringBuilder, change the character, run the toString and repeat this over and over again, instead of doing it in "batch".

  • Thank you for your contribution, and forgive the ignorance: what would "do in batch"?

  • It was a short way of saying that all characters should be changed and only then the content should be converted to String. I wrote this way because the space of the commentary ended and I didn’t want to write 2... :S

  • Quiet. Grateful once again. =)

0

In general I end up using Stringbuilder:

var theString = "ABCDEFGHIJ";
var aStringBuilder = new StringBuilder(theString);
aStringBuilder.Remove(3, 2);
aStringBuilder.Insert(3, "ZX");
theString = aStringBuilder.ToString();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.