Null \\u0000 in Java String?

2020-02-12 10:03发布

问题:

Are there any good reasons not to use \u0000 as a delimiter within a Java String? I would be encoding and decoding the string myself.

This is for saving a list of user-inputted (I'm expecting input to be typed?) strings to an Eclipse preference and reading it back. The list may be variable size so I don't think I can save each item to its own preference.

回答1:

There used to be some libraries which erroneously handled Java strings as null terminated. I don't know if it's still true but it's worth keeping such things in mind. Especially if you interop with external libraries that will handle strings as null terminated.



回答2:

If the data stays in Java, why don't you use an array or a List instead?



回答3:

Well, there's the possibility that anything transforming the string in some way may strip it. Oh, and the faint possibility that you might want to keep any nulls in the input.

What are you going to do with it?



回答4:

If you need to parse it later the string parsing functions may not accept null as a value for the delimiter.



回答5:

Sounds like you are trying to use it to store a list. Why not use ArrayList<String> ? Having a \u0000 in a String is bad. Consider using a byte array.

As you say its for saving something in the eclipse settings, i wouldn't use embedded NULs, since the files seem to be user-readable (in my ~/.eclipse at least). What do you want to save? You could stringize the items ("item 2" "item 2") for example. Just don't complicate it too much.



标签: java string null