Newsgroup: it.comp.java
Titolo: Help! Single character replace

Hi everyone,
Can someone please let me know as soon as possible if there's a way / method
to change a certain char in a String at a certain position (only once)?  I
think it just may be an algorithm brain bender for me, but I can't see how
we can mix indexOf with replace.
So instead of:
aString = aString.replace(charA, charB);
I'd like to:
aString = aString.replace(charA, charB, position);
// I know there's no such thing, so please translate into something real!
Please advise if I don't make sense!
Many thanks.
:)

 wrote in message
news:9nfrfh$1ar$1...@bugstomper.ihug.com.au...

You can use a StringBuffer, which has a setCharAt() method:
    StringBuffer buffer = new StringBuffer(aString);
    buffer.setCharAt(index, charB);
    aString = buffer.toString();
HTH,
Michiel

 wrote in message
news:xZKm7.35654$fU2.5...@news.quicknet.nl...


 wrote in message
news:9nfrfh$1ar$1...@bugstomper.ihug.com.au...


First, I make no claim as to this being the best or most efficient way. I
only calim that this code works. I gaurantee there are better ways to do
any or all of what I demonstrate below.
Below is an example of how to do what you ask. It will work not only with
a single char replacement, but with a full string as well.
public class ShortVersion {
        public static void main(String [] args) {
                if (args.length != 3) {
                        System.out.println("Usage: java ShortVersion {text} {search_phrase} {replace_phrase}");
                        return;
                }
                String text = args[0];
                String search = args[1];
                String replace = args[2];
                text = text.indexOf(search) == -1 ? text : text.substring(0,text.indexOf(search)) + replace + text.substring(text.indexOf(search) + search.length());
                System.out.println(text);
        }
}
Here is an example of a bit more robust version of the above.
Demonstating how to perform the replacement once, many or all times.
public class LongVersion {
        public static void main(String [] args) {
                if (args.length != 3) {
                        System.out.println("Usage: java LongVersion {text} {search_phrase} {replace_phrase}");
                        return;
                }
                String text = args[0];
                String search = args[1];
                String replace = args[2];
                System.out.println("replace once:" + getReplacementOnce(text, search, replace));
                System.out.println("twice:" + getReplacementCount(text,search,replace,2));
                System.out.println("all: " + getReplacementAll(text,search,replace));
        }
        public static String getReplacementAll(String text, String search, String replace) {
                boolean repeat = true;
                String newtext = new String("");
                while( repeat ) {
                        newtext = getReplacementOnce(text,search,replace);
                        repeat = !text.equals(newtext);
                        text = newtext;
                }
                return text;
        }
        public static String getReplacementCount(String text, String search, String replace, int count) {
                String newtext = text;
                while (count > 0) {
                        newtext = getReplacementOnce(text,search,replace);
                        count = (text.equals(newtext) ? 0 : --count);
                        text = newtext;
                }
                return text;
        }
        public static String getReplacementOnce(String text, String search, String replace) {
                return ( text.indexOf(search) == -1 ? text : text.substring(0,text.indexOf(search)) + replace + text.substring(text.indexOf(search) + search.length()) );
        }
}