如何在字符串的末尾和/或開頭保留空格?

[英]How to keep the spaces at the end and/or at the beginning of a String?


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 ??

還是像使用&amp一樣?為了“&”這個角色?

14 个解决方案

#1


410  

Even if you use string formatting, sometimes you still need white spaces at the beginning or the end of your string. For these cases, neither escaping with \, nor xml:space attribute helps. You must use HTML entity &#160; for a whitespace.

即使您使用字符串格式,有時您仍然需要在字符串的開頭或結尾使用空格。對於這些情況,使用\和xml:space屬性都沒有幫助。您必須使用HTML實體 為一個空格。

Use &#160; for non-breakable whitespace.
Use &#032; for regular space.

使用& # 160;non-breakable空格。使用& # 032;定期的空間。

#2


396  

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

我在這個問題報告中找到了一個解決方案: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.

這和杜西的建議是一樣的。在XML中直接插入\u0020,以保留一個空白。

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.

替換是在構建時完成的,因此不會影響游戲的性能。

#3


103  

This documentation suggests quoting will work:

該文件表明,引用將有效:

<string name="my_str_spaces">" Before and after? "</string>

#4


31  

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

我只是在字符串中使用UTF代碼的空間“\u0020”。xml文件。

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

works great. (Android loves UTF codes)

偉大的工作。(安卓愛UTF代碼)

#5


8  

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


6  

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


5  

use "" with the string resource value.

使用“”與字符串資源值。

Example : "value with spaces"

示例:“帶有空格的值”

OR

use \u0020 code for spaces.

空間使用\u0020代碼。

#8


2  

Working well I'm using \u0020

我用的是\u0020

<string name="hi"> Hi \u0020 </string>
<string name="ten"> \u0020 out of 10  </string>
<string name="youHaveScored">\u0020 you have Scored \u0020</string>

Java file

Java文件

String finalScore = getString(R.string.hi) +name+ getString(R.string.youHaveScored)+score+ getString(R.string.ten);
               Toast.makeText(getApplicationContext(),finalScore,Toast.LENGTH_LONG).show();

Screenshot here Image of Showing Working of this code

這里的屏幕截圖顯示了這個代碼的工作

#9


1  

All answers here did not work for me. Instead, to add a space at the end of a string in XML i did this

這里所有的答案對我都不起作用。相反,要在XML中添加一個字符串,我就這樣做了。

<string name="more_store">more store<b> </b> </string>

#10


1  

An argument can be made for adding the space programmatically. Since these cases will be often used in concatenations, I decided to stop the madness and just do the old + " " +. These will make sense in most European languages, I would gather.

可以通過編程方式添加空間。由於這些情況經常用於連接,我決定停止這種瘋狂,只使用舊的+“”+。我想,這些在大多數歐洲語言中都是有意義的。

#11


0  

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

這實際上可能無法回答這個問題(如何在XML中保留空格),但它可以更優雅地解決底層問題。

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

使用格式字符串連接,而不是只依賴於XML資源。首先移除白空間

<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();

#12


0  

If you need the space for the purpose of later concatenating it with other strings, then you can use the string formatting approach of adding arguments to your string definition:

如果您需要空間用於以后將其與其他字符串連接,那么您可以使用字符串格式方法,將參數添加到您的字符串定義中:

<string name="error_">Error: %s</string>

Then format the string as:

然后將字符串格式化為:

String message = context.getString(R.string.error_, "Something went wrong")

#13


-1  

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:

我並不特別了解Android,但是這看起來像通常的XML空白處理——元素中的前導和后導空白通常被認為是不重要的,並且被刪除了。嘗試xml:空間:

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

#14


-7  

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

它不支持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);

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/10/19/db492a099feae596221aebf4f85e5ecb.html



 
  © 2014-2022 ITdaan.com 联系我们: