0

I have to open a webview which loads a dynamic url based on certain parameters, I have to create the url based on different strings.

What I'd like to do is the following:

String webPage = "www.webpage.com/" + "stringA/" + "stringB/"

-> where stringA and stringB are selected based on given conditions.

Is there any way of achieving this?

I have found this idea, where the author discusses the employment of Resources#getIdentifier() vs. using reflection. Since performance is an issue for my use case, I am looking for an approach with a good runtime behavior.

Lars Gendner
  • 1,816
  • 2
  • 14
  • 24
  • did you mean `String webPage = "www.webpage.com/"; webPage+=string1; webPage+=string2; ...` – Youcef LAIDANI Mar 13 '18 at 14:44
  • What do you mean by "certain parameters"? Variable values in your code? Device configuration like screen orientation or display size? – Tobias Uhmann Mar 13 '18 at 14:49
  • 1
    In case you mean the second scenario the "bad example" you referenced would be the way to go. Note that the 3.2 seconds were measured when running the code 10,000 times. I assume in your case 0.32ms access time is absolutely acceptable. – Tobias Uhmann Mar 13 '18 at 14:55
  • 1
    I would recommend using the described approach (``Resources#getIdentifier()'') and see if it behaves well, before optimizing code in advance. @xvlcw's previous comment holds true IMHO. – Lars Gendner Mar 13 '18 at 15:00
  • @YCF_L, no, i mean just that -> /string1/string2 – user3909319 Mar 13 '18 at 15:15
  • @xvlcw - i mean both language code and country code , then have different string1 and string2 values ; in my case there are about 200 url strings, i think the identifier solution may be applicable in this case but i was curious if there is another way – user3909319 Mar 13 '18 at 15:16

2 Answers2

1
String webPage = "www.webpage.com/" + "stringA/" + "stringB/";

is just fine. However to preserve a bit of memory, you could use StringBuffer or StringBuilder, like (new StringBuilder("www.webpage.com/").append("stringA/").append("stringB/").toString();

You could also use "%s %s %s" with a String.format() and fill it dynamically with the correct values (this applies especially with string templates in resources xml)

caketuzz
  • 126
  • 4
0

what about putting the host in to the Strings.xml with place holders, like thekey = "www.webpage.com/%1$s/%2$s".

Then, call Context.getString(thekey, stringA, stringB).

String.format is another option for it.

jarly
  • 231
  • 1
  • 4
  • 16