How to keep the spaces at the end and/or at the be

2019-01-04 16:10发布

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 in this documentation link

or is it something like using &amp ; for the "&" character ??

15条回答
甜甜的少女心
2楼-- · 2019-01-04 16:37

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);
查看更多
女痞
3楼-- · 2019-01-04 16:41

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 : https://github.com/iBotPeaches/Apktool/issues/124

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.

查看更多
够拽才男人
4楼-- · 2019-01-04 16:42

I just use the UTF code for space "\u0020" in the strings.xml file.

<string name="some_string">\u0020The name of my string.\u0020\u0020</string>

works great. (Android loves UTF codes)

查看更多
祖国的老花朵
5楼-- · 2019-01-04 16:42

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);
查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-01-04 16:45

There is possible to space with different width:

<string name="space_demo">|&#x20;|&#x2009;|&#x200A;||</string>

| SPACE | THIN SPACE | HAIR SPACE | no space |

Visualisation:

enter image description here

查看更多
祖国的老花朵
7楼-- · 2019-01-04 16:45

This may not actually answer the question (How to keep whitespaces in XML) but it may solve the underlying problem more gracefully.

Instead of relying only on the XML resources, concatenate using format strings. So first remove the whitespaces

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

And then build your string differently:

String message_all_pairs_found = 
      String.format(Locale.getDefault(), 
                    "%s %d %s", 
                    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();
查看更多
登录 后发表回答