Android 如何在字符串的末尾和/或开头保留空格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1587056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to keep the spaces at the end and/or at the beginning of a String?
提问by Hubert
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) ...
但是第一个字符串末尾和第二个字符串开头的空格已经消失(显示 Toast 时)......
What should I do ?
我该怎么办 ?
I guess the answer is somewhere here in this documentation link
我想答案就在此文档链接中的某处
or is it something like using & ;
for the "&" character ??
还是类似于& ;
用于“&”字符??
回答by duessi
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  
for a whitespace.
即使您使用字符串格式,有时您仍然需要在字符串的开头或结尾使用空格。对于这些情况,使用\
或xml:space
属性转义都无济于事。您必须使用 HTML 实体 
作为空格。
Use  
for non-breakable whitespace.
Use  
for regular space.
使用 
非易碎的空白。
使用 
正规的空间。
回答by DavGin
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.
这与杜西建议的想法相同。\u0020
直接在 XML 中插入要保留的空白。
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.
替换是在构建时完成的,因此不会影响游戏的性能。
回答by Skywalker5446
This documentationsuggests quoting will work:
本文档建议引用将起作用:
<string name="my_str_spaces">" Before and after? "</string>
回答by Jasen
I just use the UTF code for space "\u0020" in the strings.xml file.
我只是在strings.xml 文件中使用UTF 代码作为空格“\u0020”。
<string name="some_string">\u0020The name of my string.\u0020\u0020</string>
works great. (Android loves UTF codes)
效果很好。(Android 喜欢 UTF 编码)
回答by kyay
This question may be old, but as of now the easiest way to do it is to add quotation marks. For example:
这个问题可能很老,但到目前为止,最简单的方法是添加引号。例如:
<string name="Toast_Memory_GameWon_part1">"you found ALL PAIRS ! on "</string>
<string name="Toast_Memory_GameWon_part2">" flips !"</string>
回答by Andrey
回答by Jeremy Logan
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 formattingfor 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);
回答by Bhupendra Joshi
use "" with the string resource value.
将 "" 与字符串资源值一起使用。
Example : "value with spaces"
示例:“带空格的值”
OR
或者
use \u0020 code for spaces.
使用 \u0020 代码表示空格。
回答by Tarsbir Singh
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();
Screenshothere Image of Showing Working of this code
此处的屏幕截图显示此代码的工作的图像
回答by MPelletier
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 mostEuropean languages, I would gather.
可以提出以编程方式添加空间的论据。由于这些案例将经常用于串联,我决定停止疯狂,只做旧的+ " " +
. 我想这些在大多数欧洲语言中都是有意义的。