How to remove nul characters (\0) from string in J

2020-07-06 06:37发布

I understand that this code in C# is trying to remove nul characters (\0) from a string.

string.Join("", mText.Split(new string[] { "\0" }, StringSplitOptions.None));

Is there any way to do that efficiently in Java?

标签: java
2条回答
三岁会撩人
2楼-- · 2020-07-06 07:32

In Java 8+ you could use StringJoiner and a lambda expression like

String str = "abc\0def";
StringJoiner joiner = new StringJoiner("");
Stream.of(str.split("\0")).forEach(joiner::add);
System.out.println(str);
System.out.println(joiner);

Output is

abc
abcdef
查看更多
相关推荐>>
3楼-- · 2020-07-06 07:33

You can write:

mText.replace("\0", "");
查看更多
登录 后发表回答