将ByteBuffer转换为String是否删除位于String末尾的字符串换行符? [英] Converting ByteBuffer To String remove linebreaks of string which are located at the end of String?

查看:627
本文介绍了将ByteBuffer转换为String是否删除位于String末尾的字符串换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在android studio中写了以下代码

I write the following code in the android studio

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String hello = "Hello\n\n";
        ByteBuffer buffer2 = ByteBuffer.allocate(hello.getBytes().length);
        buffer2.put(hello.getBytes());
        buffer2.flip();
        while (true) {
            Log.d(TAG, new String(buffer2.array()));
        }
    }
}

它应该打印带有换行符的Hello,但不显示以下行截图.

It should print Hello with line breaks but as shown in the following screenshot it doesn't.

推荐答案

将ByteBuffer转换为String可以删除以下字符串的换行符:位于String的末尾?

Converting ByteBuffer To String remove linebreaks of string which are located at the end of String?

答案:否

演示:

import java.nio.ByteBuffer;

public class Main {
    public static void main(String[] args) {
        String hello = "Hello\n\n";
        ByteBuffer buffer2 = ByteBuffer.allocate(hello.getBytes().length);
        buffer2.put(hello.getBytes());
        buffer2.flip();

        System.out.println(new String(buffer2.array()) + "Hi");
    }
}

输出:

Hello

Hi

这篇关于将ByteBuffer转换为String是否删除位于String末尾的字符串换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆