添加“\0”后,是否可以将数据添加到字符串中(空值)? [英] Is it possible to add data to a string after adding "\0" (null)?
问题描述
我有一个我正在创建的字符串,我需要在字符串中添加多个\0(空)字符。在每个空字符之间,是其他文本数据(Just ASCII字母数字字符)。
I have a string that I am creating, and I need to add multiple "\0" (null) characters to the string. Between each null character, is other text data (Just ASCII alphanumeric characters).
我的问题是在J2SE中添加第一个null(\0)时,java然后似乎确定它是一个字符串终止符(类似于C ++),并忽略所有其他附加数据。不会引发错误,只会忽略尾随数据。我需要在字符串中的null之后强制添加其他尾随数据。我必须为我支持的遗留数据库执行此操作。
My problem is that in J2SE when you add the first null (\0), java then seems to determine that it's a string terminator, (similar to C++), and ignores all other data being appended. No error is raised, the trailing data is just ignored. I need to force the additional trailing data after a null in the string. I have to do this for a legacy database that I am supporting.
我试图对字符串进行编码/解码,希望像%00这样的内容会欺骗字符串行为,但是当我重新编码字符串时,Java再次看到空字符,并在第一个空字符后删除所有数据。
I have tried to encode/decode the string in hoping that something like %00 would fool the interpretation of the string behaviour, but when I re-encode the string, Java sees the null character again, and removes all data after the first null.
更新:这是相关的代码段。是的,我正在尝试使用 Strings
。我打算尝试使用字符,但我仍然需要将它作为字符串保存到数据库中,所以我怀疑我最终会遇到同样的问题。
Update: Here is the relevant code snippet. Yes, I am trying to use Strings
. I intend to try chars, but I still have to save it into the database as a string, so I suspect that I will end up with the same problem.
一些背景知识。我通过HTTP帖子接收数据,其中包含\ n。我需要删除换行符并用\0替换它们。 debug
方法只是一个简单的方法 System.out.println
。
Some background. I am receiving data via HTTP post that has "\n". I need to remove the newlines and replace them with "\0". The "debug
" method is just a simple method that does System.out.println
.
String[] arrLines = sValue.split("\n");
for(int k=0;k<arrLines.length;k++) {
if (0<k) {
sNewValue += "\0";
}
sNewValue+= arrLines[k];
debug("New value =" + sNewValue);
}
sNewValue,一个字符串,提交到数据库,需要像以下一样完成一个字符串。当我在控制台中每次迭代后显示 sNewValue
的当前值时,我观察到的是这样的:
sNewValue, a String, is committed to the database and needs to be done as a String. What I am observing when i display the current value of sNewValue
after each iteration in the console is something like this:
输入为value1 \\\
Value2 \\\
Value3
控制台中的输出从此代码中提供给我
input is value1\nValue2\nValue3 Output in the console is giving me from this code
value1
value1
value1
我期待
value1
value1 value2
value1 value2 value3
分别在value1,value2和value3之间使用不可打印的null。请注意,实际保存回数据库的值也只是value1。因此,它不仅仅是一个控制台显示问题。 \0之后的数据被忽略。
with non-printable null between value1, value2 and value3 respectively. Note that the value actually getting saved back into the database is also just "value1". So, it's not just a console display problem. The data after \0 is getting ignored.
推荐答案
我强烈怀疑这无关紧要与字符串本身的文本 - 我怀疑它是如何显示。例如,试试这个:
I strongly suspect this is nothing to do with the text in the string itself - I suspect it's just how it's being displayed. For example, try this:
public class Test {
public static void main(String[] args) {
String first = "first";
String second = "second";
String third = "third";
String text = first + "\0" + second + "\0" + third;
System.out.println(text.length()); // Prints 18
}
}
这打印18,显示所有人物存在。但是,如果您尝试在UI标签中显示 text
,我只会看到第一个
并不会感到惊讶。 (在相当弱的调试器中也是如此。)
This prints 18, showing that all the characters are present. However, if you try to display text
in a UI label, I wouldn't be surprised to see only first
. (The same may be true in fairly weak debuggers.)
同样你应该可以使用:
char c = text.charAt(7);
现在 c
应为'e'
这是第二个的第二个字母。
And now c
should be 'e'
which is the second letter of "second".
基本上,我希望Java的核心不是关心它包含U + 0000的事实。就Java而言,它只是另一个角色。只有在与本机代码(例如显示器)的边界处,它才可能导致问题。
Basically, I'd expect the core of Java not to care at all about the fact that it contains U+0000. It's just another character as far as Java is concerned. It's only at boundaries with native code (e.g. display) that it's likely to cause a problem.
如果这没有帮助,请完整地解释 你所观察到的 - 它是什么让你认为其余的数据没有附加。
If this doesn't help, please explain exactly what you've observed - what it is that makes you think the rest of the data isn't being appended.
编辑:另一种诊断方法是打印出来字符串中每个字符的Unicode值:
Another diagnostic approach is to print out the Unicode value of each character in the string:
for (int i = 0; i < text.length(); i++) {
System.out.println((int) text.charAt(i));
}
这篇关于添加“\0”后,是否可以将数据添加到字符串中(空值)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!