Java 中的字符无法正确显示

发布于 2024-08-10 02:02:44 字数 457 浏览 7 评论 0原文

我尝试过“&nbsp;&nbsp;”在标准输出 Java 字符串中显示两个空格。尝试 System.out.println("__"); <---- (两个空格,但显然,它会将其修剪为一个空格,因此有下划线)

我想有一个逃避 &nbsp; 的方法,但我无法弄清楚也无法在网上找到帮助。搜索它是很讽刺的,因为大量的文字 &nbsp;&nbsp; 出现了。

任何帮助将不胜感激。

编辑:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

杀お生予夺 2024-08-17 02:02:44

  是 HTML 特定的编码。

您第一次是对的 - 对应于两个空格的 Java 字符串只是两个空格字符,例如

String s = "  ";

您尝试的 println() 调用应该已经工作。你做了什么让你相信它被修剪成一个空间?我认为你的问题在其他地方......

编辑:

根据你的代码片段 - 是 COLUMNS 5,有可能吗? :-)

再次编辑:

好的,如果 COLUMNS 是 15,那么此代码将导致 r 附加 20 个空格。如果您想真正确定,可以在调试器中单步执行,或者在 r += 行上方放置一条日志语句,以确保该语句被调用了多少次。

在将其输出打印到您正在检查的位置之前,还要看看稍后如何使用 r ;也许它的值在某个时刻被截断,要么在 Java 中显式截断,要么甚至隐式截断(例如存储在数据库列中,该列在稍后检索和显示之前太窄了 10 个字符)。

  is an HTML-specific encoding.

You were right first time - the Java string that corresponds to two spaces is simply two space characters, e.g.

String s = "  ";

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 the r += 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).

花之痕靓丽 2024-08-17 02:02:44

如果您想在字符串中使用不间断空格,无论出于何种原因,您都必须使用 Unicode 文字:

System.out.println("banana\u00A0phone");

If you want a non-breaking space in your string, for whatever reason, you have to use the Unicode literal:

System.out.println("banana\u00A0phone");
弃爱 2024-08-17 02:02:44

您不需要在 Java 中使用  。 System.out.print(" "); 应该可以解决问题。尝试类似的方法:

System.out.print("This is the a string before spaces" + "  " + "this is a string after spaces");

You don't need to use   in Java. System.out.print(" "); should do the trick. Try something like:

System.out.print("This is the a string before spaces" + "  " + "this is a string after spaces");
囚我心虐我身 2024-08-17 02:02:44

您还可以通过按住 Alt 键并在数字键盘上键入 0160 来创建不间断空格。

You can also create a non-breaking space by holding the Alt key and typing 0160 on the number pad.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文