Print string with null character in java
Asked Answered
I

3

6

I have string which contains null character ie \0. How can I print the whole string in java?

String s = new String("abc\u0000def");
System.out.println(s.length());

System.out.println(s);

Output on eclipse console:

7
abc

Length is that of complete string, but how can I print the whole string?

UPDATE: I am using

Eclipse Helios Service Release 2

Java 1.6

Idell answered 11/3, 2013 at 11:29 Comment(7)
Works for me (Eclipse console), output is 7<nl>abc def - on what system, and on which console, did you start the program?Abdias
It prints abc def. What else do you expect it to print? (There is no space actually between abc & def, its a null char there, but it prints though)Nephology
so you want \u0000 in output?Monosepalous
btw, there's no good reason to write new String("abc\u0000def") in this context. "abc\u0000def" would suffice.Roentgenograph
Do you want abc def as output or do you literally want abc\u0000def?Pessimist
@Andreas I have updated my question, please have a lookIdell
@Pessimist I just want that all the printable chars are printed, and whatever is printed for unprintable chars.Idell
P
2

If Eclipse won't cooperate, I'd suggest replacing the null characters with spaces before printing:

System.out.println(s.replace('\u0000', ' '));

If you need to do this in a lot of places, here's a hack to filter them from System.out itself:

import java.io.*;

...

System.setOut(new PrintStream(new FilterOutputStream(
        new FileOutputStream(FileDescriptor.out)) {
    public void write(int b) throws IOException {
        if (b == '\u0000') b = ' ';
        super.write(b);
    }
}));

Then you can call System.out methods normally, with all the data going through the filter.

Photoengraving answered 11/3, 2013 at 16:14 Comment(0)
V
3

Converting the String to char array is an alternative. This works for me:

System.out.println(s.toCharArray());

Which outputs abcdef to the console (eclipse).

Virgy answered 11/3, 2013 at 11:43 Comment(0)
P
2

If Eclipse won't cooperate, I'd suggest replacing the null characters with spaces before printing:

System.out.println(s.replace('\u0000', ' '));

If you need to do this in a lot of places, here's a hack to filter them from System.out itself:

import java.io.*;

...

System.setOut(new PrintStream(new FilterOutputStream(
        new FileOutputStream(FileDescriptor.out)) {
    public void write(int b) throws IOException {
        if (b == '\u0000') b = ' ';
        super.write(b);
    }
}));

Then you can call System.out methods normally, with all the data going through the filter.

Photoengraving answered 11/3, 2013 at 16:14 Comment(0)
M
1

the correct output of your code using Java 5 or higher is

public class TestMain
{
    public static void main(String args[])
    {
        String s = new String("abc\u0000def");
        System.out.println(s.length());
        System.out.println(s);
    }
}

7
abc def

Maleate answered 11/3, 2013 at 11:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.