Java 字符串中的空 \u0000?
有什么好的理由不使用 \u0000 作为 Java 字符串中的分隔符吗? 我会自己编码和解码字符串。
这是为了将用户输入的(我期望输入的内容?)字符串列表保存到 Eclipse 首选项中并读取它。 该列表的大小可能是可变的,因此我认为我无法将每个项目保存为自己的偏好。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果数据保留在 Java 中,为什么不使用数组或列表呢?
If the data stays in Java, why don't you use an array or a List instead?
曾经有一些库错误地将 Java 字符串处理为 null 终止。 我不知道这是否仍然正确,但值得牢记这些事情。 特别是当您与外部库进行互操作时,这些库会将字符串处理为空终止。
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.
好吧,任何以某种方式转换字符串的东西都有可能会剥离它。 哦,还有一种微弱的可能性,您可能想在输入中保留任何空值。
你打算用它做什么?
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?
如果您稍后需要解析它,字符串解析函数可能不接受 null 作为分隔符的值。
If you need to parse it later the string parsing functions may not accept null as a value for the delimiter.
听起来您正在尝试使用它来存储列表。 为什么不使用 ArrayList? 在字符串中包含
\u0000
是不好的。 考虑使用字节数组。正如您所说,它是为了在 Eclipse 设置中保存某些内容,我不会使用嵌入式 NUL,因为这些文件似乎是用户可读的(至少在我的 ~/.eclipse 中)。 你想保存什么? 例如,您可以对项目进行字符串化(“项目 2”“项目 2”)。 只是不要让事情变得太复杂。
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.