Enforcing spaces in string resources

AndroidString

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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".

Solution 2 - Android

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).

Solution 3 - Android

Just quote the value, like this

<string name="str_name">" Hello World!"</string>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSK9View Question on Stackoverflow
Solution 1 - AndroidAndreas KlöberView Answer on Stackoverflow
Solution 2 - AndroidLasse SamsonView Answer on Stackoverflow
Solution 3 - AndroidTang ChanrithView Answer on Stackoverflow