2

I'm currently having trouble to replace all occurrences of 'x' in my string to a number.

I already tried: myString.replace('x',Integer.toString(i)) and stuff like that with "x", and instead of the Integer to string, replacing a number in quote, and it don't work, the string remains the same.

then I made the following function:

public static void trocaXporDouble(String a, double c){
            int i;
            for(i=0;i<a.length();i++){
                if(a.charAt(i)=='x'){
                    String newStr1 = a.substring(0,i-1);
                    String newStr = a.substring(i+1,a.length());
                    newStr1.concat(Double.toString(c));
                    newStr1.concat(newStr);
                }
            }
}

but even with that function it is still not working, can someone give me a hand here?

Thanks in advance.

0

5 Answers 5

10

Unlike C, in java strings are immutable - you can't change them: Calling replace() creates a new String with the changes made.

Instead, you need to assign the result of the replace() call back to the variable.

ie

myString.replace("x", "9"); // does nothing to myString

myString = myString.replace("x", "9"); // works
Sign up to request clarification or add additional context in comments.

4 Comments

It actually does something, but you do not save the result, right? I guess the compiler will "optimize it away".
The first statement actually replaces the X with 9 you just don't assign its value to a reference.
Yes, it does do something, but the result is not accessible without a pointer to it.
Perhaps I should have said "does nothing useful". Anyway, to please the pedants, I've narrowed the comment.
1

It works .You must doing something wrong with replace

String s = "abcxsdx xdf";
String after = s.replace("x","1");
System.out.println(after);   //prints   abc1sd1 1df

Comments

1

Here you go. Recursive method, no constraint on the wildcard position, you can choose the replacement characters ("01" for binary count for instance, or "0123456789" for all naturals.)

import java.util.ArrayList;

public class Main {

    public static void main(String[] args) {
        String input = "base 3 : **";    // the input, with wildcard
        String replacable = "*";         // wildcard char
        String replacedBy = "0123";      // all the possibilities for wildcard replacement

        ArrayList<String> output = genCombinations(input, replacable, replacedBy);

        // just print the results
        for(String s : output)
            System.out.println(s);
    }

    private static ArrayList<String> genCombinations(String input,String replacable,  String replacement) {
        StringBuilder sb = new StringBuilder(input);
        ArrayList<String> out = new ArrayList<>(); //may warn with jdk < 1.7

        // find the first wildcard
        int index = input.indexOf(replacable);
        if(index==-1){
            //no wildcard found
            out.add(sb.toString());
            return out;
        }

        for(int i = 0; i<replacement.length(); ++i){
            char c = replacement.charAt(i);
            sb.setCharAt(index, c);
            // gen all combination with the first wildcard already replaced
            out.addAll(genCombinations(sb.toString(),replacable, replacement));
        }

        return out;
    }

}

outputs:

base 3 : 00
base 3 : 01
base 3 : 02
base 3 : 03
base 3 : 10
base 3 : 11
base 3 : 12
base 3 : 13
base 3 : 20
base 3 : 21
base 3 : 22
base 3 : 23
base 3 : 30
base 3 : 31
base 3 : 32
base 3 : 33

Comments

0

I think this way might help

String x="ababc";
        String c=x.replaceAll("a", "x");
        System.out.println(c);

Comments

0

Syntax is to replace the char with char(replace(oldChar, newChar)) not with Integer

Try the following code and see

   myString=myString.replace("x",String.valueOf(i));

instead

  myString.replace('x',Integer.toString(i));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.