views:

1285

answers:

5

I have to concatenate these two strings from my resource/value files:

<string name="Toast_Memory_GameWon_part1">you found ALL PAIRS ! on </string>
<string name="Toast_Memory_GameWon_part2"> flips !</string>

I do it this way :

String message_all_pairs_found = getString(R.string.Toast_Memory_GameWon_part1)+total_flips+getString(R.string.Toast_Memory_GameWon_part2);

Toast.makeText(this, message_all_pairs_found, 1000).show();

BUT THE SPACES at the end of the first string and at the beginning of the second string have disappeared (when the Toast is shown) ...

What should I do ?

I guess the answer is somewhere here : http://developer.android.com/reference/java/util/regex/package-descr.html#impnotes

or is it something like using & for the "&" caracter ??

A: 

I've no idea about Android in particular, but this looks like the usual XML whitespace handling - leading and trailing whitespace within an element is generally considered insignificant and removed. Try xml:space:

<string name="Toast_Memory_GameWon_part1" xml:space="preserve">you found ALL PAIRS ! on </string>
<string name="Toast_Memory_GameWon_part2" xml:space="preserve"> flips !</string>
Pavel Minaev
Thanks for your answer Pavel. Have a good day.
Hubert
Unfortunately, `xml:space` seems to be ignored.
Thomas
A: 

It does not work with xml:space="preserve"

so I did it the quickest way =>

I simply added a +" "+ where I needed it ...

String message_all_pairs_found = getString(R.string.Toast_Memory_GameWon_part1)+" "+total_flips+" "+getString(R.string.Toast_Memory_GameWon_part2);
Hubert
+1  A: 

If you really want to do it the way you were doing then I think you have to tell it that the whitespace is relevant by escaping it:

<string name="Toast_Memory_GameWon_part1">you found ALL PAIRS ! on\ </string>
<string name="Toast_Memory_GameWon_part2">\ flips !</string>

However, I'd use string formatting for this. Something like the following:

<string name="Toast_Memory_GameWon">you found ALL PAIRS ! on %d flips !</string>

then

String message_all_pairs_found = String.format(getString(R.string.Toast_Memory_GameWon), total_flips);
fiXedd
Thank you. Hubert.
Hubert
You are most welcome. This tripped me up for a while too.
fiXedd
Escaping with a backslash does not work (for me at least). Using a format string is a great solution generally, but not always. For instance, `String.format` causes many memory allocations that are undesirable in a game.
Thomas
+2  A: 

Even if you use string formatting sometimes you still need whitespaces at the beginning or the end. On these cases neither escaping with \ nor xml:space attribute helpes. You must use xml encoding &#160;for whitespaces.

duessi
This *kind of* works, but it inserts a non-breaking space (U+00A0) instead of a regular space. That can be undesirable in some situations.
Thomas
+2  A: 

I ran into the same issue. I wanted to leave a blank at the end of a resource string representing an on-screen field name.

I found a solution on this issue report : http://code.google.com/p/android-apktool/issues/detail?id=14

This is the same idea that Duessi suggests. Insert \u0020 directly in the XML for a blank you would like to preserve.

Example :

<string name="your_id">Score :\u0020</string>

The replacement is done at build time, therefore it will not affect the performance of your game.

DavLink
Nice, works like a charm! Thanks!
Thomas