DIGIT-ARC read-only archive · snapshot: July 23, 2026

String.replace

3 posts · originally from https://geek.digit.in/community/threads/string-replace.84634/

estranged12
estranged12 Original post #1
Apr 7, 2008 at 6:52 AM
I am trying to use the string.replace function to replace one character of a string to a sequence of others.
Like this
Say i have a simple string called draw.
String draw = "F";
System.out.println(draw);
draw.replace("F","F-F+F+F-F");
System.out.println(draw);

The output is
F
F

What do I do to allow it to change the F to F-F+F+F-F (and then later all the Fs in that string to be F-F+F+F-F recursively), because the string.replace function doesnt work.

Thanks
QwertyManiac
Apr 7, 2008 at 7:49 AM
<String>.replace("Str1","Str2") RETURNS a string, and does not change the actual string.

So your code's gotta be:
Code:
class M
{
    public static void main(String args[])
    {
        String a = "Hi";
        [B]a = a.replace[/B]("F","F-F+F+F-F");
        System.out.println(a);
    }
}

By recursively do you mean that even the replaced F must be re-replaced with F-F+F+F-F? I don't see why you wish to do that infinite thing but it can be achieved via a simple While(True) kind of loop..
ruturaj3
Apr 9, 2008 at 9:58 AM
Or u can use StringBuilder instead of String class, bcoz it is mutable.