5

Is possible to concatenate a String already existing in strings.xml with the current one.
That is: If I have

<string name="hello">hello</string>

and I want to create a new string with the format "Your android says "+hello

oers
  • 18,436
  • 13
  • 66
  • 75
Addev
  • 31,819
  • 51
  • 183
  • 302

4 Answers4

24

I am afraid that is not possible in strings.xml.

What you can do is create the final string programmatically using getString:

String outStr = getString(R.string.your_android_says) + 
  " " + getString(R.string.hello);
phoenix
  • 7,988
  • 6
  • 39
  • 45
havexz
  • 9,550
  • 2
  • 33
  • 29
10

Not sure if this is what you want:

String s = "Your android says" + getResources().getString(R.id.name);

What you could also do it like:

<string name="hello">%s hello</string>

String androidsays = "Your android says";
String s = getString(R.id.name, androidsays);
Maggie
  • 7,823
  • 7
  • 45
  • 66
2

you can create any number of altered string , but bounded upto runTime memory only . you can't store persistently this new string like usually you do through values/string.xml .

example String newString = "something" + getResources().getString(id);

sharedPreferences in the way you can achieve this . store newly generated string into sharedpreferences and access later .

Shailendra Singh Rajawat
  • 8,172
  • 3
  • 35
  • 40
0

If you want to do so without having to add any Java/Kotlin code, you could use this small library I've created: https://github.com/LikeTheSalad/android-stem

It allows you to do something like this:

<string name="app_name">world</string>
<string name="welcome_message">hello ${app_name}</string>

And then the library does this at build time:

<!-- Auto generated during compilation -->
<string name="welcome_message">hello world</string>
César Muñoz
  • 535
  • 1
  • 6
  • 10