Java 字符串中的空 \u0000?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/318775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Null \u0000 in Java String?
提问by Albert
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.
是否有充分的理由不使用 \u0000 作为 Java 字符串中的分隔符?我会自己编码和解码字符串。
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.
这是为了将用户输入的(我希望输入输入?)字符串列表保存到 Eclipse 首选项并读取它。该列表可能大小不一,因此我认为我无法根据自己的喜好保存每个项目。
采纳答案by Dinah
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.
曾经有一些库错误地将 Java 字符串处理为空终止符。我不知道这是否仍然正确,但值得记住这些事情。特别是如果您与将字符串处理为空终止的外部库进行互操作。
回答by phihag
If the data stays in Java, why don't you use an array or a List instead?
如果数据保留在 Java 中,为什么不使用数组或列表呢?
回答by Jon Skeet
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?
你打算怎么办?
回答by Craig Wohlfeil
If you need to parse it later the string parsing functions may not accept null as a value for the delimiter.
如果您需要稍后解析它,字符串解析函数可能不接受 null 作为分隔符的值。
回答by Johannes Schaub - litb
Sounds like you are trying to use it to store a list. Why not use ArrayList<String>? Having a \u0000in a String is bad. Consider using a byte array.
听起来您正在尝试使用它来存储列表。为什么不使用ArrayList<String>?在\u0000字符串中有 a是不好的。考虑使用字节数组。
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.
正如您所说的用于在 eclipse 设置中保存某些内容,我不会使用嵌入式 NUL,因为这些文件似乎是用户可读的(至少在我的 ~/.eclipse 中)。你想保存什么?例如,您可以将项目字符串化(“项目 2”“项目 2”)。只是不要太复杂。

