tags:

views:

3142

answers:

5

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  A: 

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?

Jon Skeet
A: 

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

Craig Wohlfeil
+1  A: 

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

phihag
It is going to be stored into an Eclipse preference, for persistence across sessions, which accepts only Strings and primitives.
Albert
In this case, I'd serialize it, but storing it the way you proposed works as well, of cause.
phihag
+1  A: 

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.

Dinah
Thank you to Jon Skeet (http://stackoverflow.com/questions/305223/jon-skeet-facts) for allowing my answer to be accepted.
Dinah
A: 

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.

Johannes Schaub - litb
It is going to be stored into an Eclipse preference, for persistence across sessions, which accepts only Strings and primitives.Why would a byte array with a 0 be better than a String with \u0000?
Albert
because some functions may not expect embedded NULs and possibly strip them out.
Johannes Schaub - litb
It is trying to save command line arguments to be played back later. I think the delimiter will have to be something that cannot be typed on a keyboard.
Albert
doesn't eclipse provide a function to store something to its settings?
Johannes Schaub - litb
Something other than it's preferences facility?My problem is that I don't know how long this list will be so I can't use one preference each.
Albert