ByteBuffer丢弃尾随的换行符。

huangapple go评论79阅读模式
英文:

ByteBuffer discards trailing newline

问题

这是我将字符串放入ByteBuffer的方法:

String message = "Hello\n\n";
ByteBuffer bresult = ByteBuffer.allocate(message.getBytes().length);
bresult.put(message.getBytes());
bresult.flip();

当我将ByteBuffer转换为字符串以查看结果时,上述字符串中的 \n\n 被移除。
这是我将ByteBuffer转换为字符串的方式:

System.out.print(new String(bresult.array()));

结果是没有任何换行的 "Hello"。
您可以在下面的日志截图中看到结果:
[![enter image description here][1]][1]

但是,当我在 "Hello" 字符串中添加空格,比如 message = "Hello\n\n "; 结果如下:
[![enter image description here][2]][2]
如您所见,在 "Hello" 字符串下方有一些换行符。

英文:

Here is how I put a string to ByteBuffer

String message="Hello\n\n";
ByteBuffer bresult = ByteBuffer.allocate(message.getBytes().length);
bresult.put(message.getBytes());
bresult.flip();

When I convert bytebuffer to string to see the result \n\n is removed from the above string.
This how I convert ByteBuffer to String

print(new String(bresult.array()));

and the result is Hello without any line breaks.
You can see the result in the below screenshot from my log
[![enter image description here][1]][1]

but when I add spaces to hello string like message="Hello\n\n " the result is the below:
[![enter image description here][2]][2]
as you can see there are some line breaks under hello string.

答案1

得分: 4

我无法重现这个问题。以下是代码部分的翻译:

import java.nio.ByteBuffer;

public class App {

  public static void main(String[] args) {
    String str = "Hello\n\n";

    ByteBuffer buf = ByteBuffer.allocate(str.getBytes().length);
    buf.put(str.getBytes());
    buf.flip();

    String str2 = new String(buf.array());

    System.out.println(str.equals(str2));
    System.out.println(str2.endsWith("\n\n"));
  }
}

给出的输出是:

true
true

这意味着从byte[]创建的String与原始的String具有相同的字符。

一些注意事项:

  1. 上面使用ByteBuffer的方式是一个绕弯路,实际上等同于str2 = new String(str.getBytes())。我使用ByteBuffer是因为这是你在问题中使用的方式。

  2. 要小心使用String#getBytes()String#<init>(byte[])。它们都使用默认的平台编码,这可能会导致问题。考虑显式指定编码方式。

  3. 如果我用System.out.print(str2)替换测试部分,我会得到以下输出:

    Hello
    
    

    这是"Hello"后面跟着两个换行符。如果使用println,则会有三个换行符。请注意,换行符通常不会直接可见。

英文:

I cannot reproduce the problem. The following:

import java.nio.ByteBuffer;

public class App {

  public static void main(String[] args) {
    String str = &quot;Hello\n\n&quot;;

    ByteBuffer buf = ByteBuffer.allocate(str.getBytes().length);
    buf.put(str.getBytes());
    buf.flip();

    String str2 = new String(buf.array());

    System.out.println(str.equals(str2));
    System.out.println(str2.endsWith(&quot;\n\n&quot;));
  }
}

Gives this output:

true
true

Which means the String created from the byte[] has all the same characters as the original String.

Some notes:

  1. The use of ByteBuffer above is a roundabout way of doing str2 = new String(str.getBytes()). I used ByteBuffer since that's what you used in the question.

  2. Be careful with String#getBytes() and String#&lt;init&gt;(byte[]). Both use the default platform encoding which may or may not cause problems. Consider specifying the encoding explicitly.

  3. If I replace the test with System.out.print(str2) I get the following output:

    Hello
    
    

    That's "Hello" followed by two line breaks. If println was used instead then there would be three line breaks. Note that line breaks are not typically directly visible.

huangapple
  • 本文由 发表于 2020年7月22日 13:50:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63027747.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定