Java 中的字符无法正确显示
我尝试过“
”在标准输出 Java 字符串中显示两个空格。尝试 System.out.println("__");
<---- (两个空格,但显然,它会将其修剪为一个空格,因此有下划线)
我想有一个逃避
的方法,但我无法弄清楚也无法在网上找到帮助。搜索它是很讽刺的,因为大量的文字
出现了。
任何帮助将不胜感激。
编辑:
for (int j = 0; j < COLUMNS; j++)
if (j < 10){
r += "__";
}
产生 10 个空格,而不是打印时预期的 20 个空格
抱歉,我对这里的格式设置还是新手
I have tried "
" to display two spaces in a standard output Java String. Trying System.out.println("__");
<---- (two spaces, but, obviously, it trims it down to one space, hence the underscore)
I imagine there is a way to escape the
, but I can not figure it out nor find help online. Searching for it is ironic because a lot of literal
show up.
Any help would be appreciated.
EDIT:
for (int j = 0; j < COLUMNS; j++)
if (j < 10){
r += "__";
}
produces 10 spaces, not 20 like expected when printed
sorry I am still new at formatting here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您第一次是对的 - 对应于两个空格的 Java 字符串只是两个空格字符,例如
您尝试的 println() 调用应该已经工作。你做了什么让你相信它被修剪成一个空间?我认为你的问题在其他地方......
编辑:
根据你的代码片段 - 是
COLUMNS
5,有可能吗? :-)再次编辑:
好的,如果 COLUMNS 是 15,那么此代码将导致
r
附加 20 个空格。如果您想真正确定,可以在调试器中单步执行,或者在r +=
行上方放置一条日志语句,以确保该语句被调用了多少次。在将其输出打印到您正在检查的位置之前,还要看看稍后如何使用
r
;也许它的值在某个时刻被截断,要么在 Java 中显式截断,要么甚至隐式截断(例如存储在数据库列中,该列在稍后检索和显示之前太窄了 10 个字符)。You were right first time - the Java string that corresponds to two spaces is simply two space characters, e.g.
The println() call that you tried ought to have worked. What did you do that led you to believe it was trimmed down to a single space? I think your problem is elsewhere...
EDIT:
Based on your code snippet - is
COLUMNS
5, by any chance? :-)EDIT AGAIN:
OK, if COLUMNS is 15 then this code will result in
r
having twenty spaces appended to it. If you want to be really sure you can either step through in a debugger or put a logging statement above ther +=
line to see for sure how many times the statement is called.Also have a look at how
r
is used later on before its output is printed to the place you're inspecting; perhaps its value is truncated at some point, either explicitly in Java or perhaps even implicitly (such as being stored in a database column that's 10 characters too narrow before being retrieved and displayed later).如果您想在字符串中使用不间断空格,无论出于何种原因,您都必须使用 Unicode 文字:
If you want a non-breaking space in your string, for whatever reason, you have to use the Unicode literal:
您不需要在 Java 中使用 。
System.out.print(" ");
应该可以解决问题。尝试类似的方法:You don't need to use in Java.
System.out.print(" ");
should do the trick. Try something like:您还可以通过按住 Alt 键并在数字键盘上键入 0160 来创建不间断空格。
You can also create a non-breaking space by holding the Alt key and typing 0160 on the number pad.