Hi, I would like to know how to set a character of an String object.
It's just like the opposite of charAt() method which returns the char
at a given position.
You could either get the prefix and suffix subsequences and put the
new char in the middle as such:
index i = 1;
String abc = "abc";
String adc = abc.substring(0, i) + "d" + abc.substring(i+1,
abc.length);
This is untested though and may throw IndexOutOfBounds if the index is
= length.
Or you could put this string in a StringBuffer and use its
setCharAt(int index, char ch) method.
index i = 1;
String abc = "abc";
StringBuffer abcBuffer = new StringBuffer(abc);
abcBuffer.setCharAt(i, 'd');
String adc = abcBuffer.toString();
There may be other ways to solve this, but these seemed like fairly
simple ways to do it, the second being simpler/safer it seems.
Scott