87

In an Android string resource, how do I enforce a space?

For example, when I do " result" rather than "result" I still see "result" in my application's text view.

Ken
  • 30,811
  • 34
  • 116
  • 155

3 Answers3

160

Did you try to surround your string with quotes? Maybe leading and trailing whitespaces are stripped automatically.

<string name="foo">" bar"</string>

See the example at https://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling in section "Escaping apostrophes and quotes".

Pang
  • 9,564
  • 146
  • 81
  • 122
Andreas Klöber
  • 5,855
  • 2
  • 27
  • 20
101

As an alternative to the answer given by Andreas Klöber you can use this method:

<string name="foobar">foo \u0020 bar</string>

This utilizes the unicode space character (\u0020).

Lasse Samson
  • 1,752
  • 3
  • 18
  • 17
-8

Just quote the value, like this

<string name="str_name">" Hello World!"</string>
Abandoned Cart
  • 4,512
  • 1
  • 34
  • 41
Tang Chanrith
  • 1,219
  • 12
  • 8