Martin R. L. Martin R. L. - 11 months ago 39
Java Question

Get strings to replace parts of one other string

I'm trying to make two strings replace two parts of one other string, and then load the combination of the url and replaced string-parts. Someone knows how to do that?

I have come this far, but this is obviously not working.

String website = ""
String b = preferences.getString("bKey", "Error");
String g = preferences.getString("gKey", "Error");
String replaced = website.replace("b", b);
String replaced = website.replace("g", g);

Answer Source

You may encounter problems with the code you want to use. If you have something like this: "", and you apply your replace method, it will give you a confusing code. Therefore, you need to specify a better parameter to replace your url, maybe you can use a model like: "{b}/{g}" and in your replace method use "{b}" and "{g}". By the way, your only mistake was to define twice "replaced", the second time you should just use:

replaced = replaced.replace("{g}", g);