简体   繁体   中英

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

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?

你可以写:

mText.replace("\0", "");

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM