4

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.

Albert
  • 536
  • 3
  • 7
  • 16

5 Answers5

3

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

phihag
  • 278,196
  • 72
  • 453
  • 469
  • It is going to be stored into an Eclipse preference, for persistence across sessions, which accepts only Strings and primitives. – Albert Nov 25 '08 at 20:54
  • In this case, I'd serialize it, but storing it the way you proposed works as well, of cause. – phihag Nov 25 '08 at 22:44
2

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
  • 52,922
  • 30
  • 133
  • 149
  • Thank you to Jon Skeet (http://stackoverflow.com/questions/305223/jon-skeet-facts) for allowing my answer to be accepted. – Dinah Nov 26 '08 at 04:29
1

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
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

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

Craig Wohlfeil
  • 627
  • 9
  • 9
0

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
  • 496,577
  • 130
  • 894
  • 1,212
  • 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 Nov 25 '08 at 20:56
  • because some functions may not expect embedded NULs and possibly strip them out. – Johannes Schaub - litb Nov 25 '08 at 20:59
  • 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 Nov 25 '08 at 21:00
  • doesn't eclipse provide a function to store something to its settings? – Johannes Schaub - litb Nov 25 '08 at 21:02
  • 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 Nov 25 '08 at 21:03