"Hello" .indexOf(“")返回0 [英] "Hello".indexOf("") returns 0
问题描述
如果我尝试在String中找到空字符的索引,我只是试图找出编译器的工作方式.我记得有人总是将空字符附加在字符串的末尾,所以我期望代码下面给我5.
I Just tried to find out what the compiler does if I try to find the index of a null charachter in a String.I remember somehwere that a null charachter is always appended to the end of a string, so i expected the code below to give me 5.
当我输入以下内容时:
String s = "Hello";
System.out.println(s.indexOf(""));
输出为0
请帮助!
推荐答案
首先,如果您要查找空字符,则可能要执行(char)0
,因为"
是一个空字符串(无字符).
First, if you were looking for a null character you would probably want to do (char)0
because ""
is an empty string (no character).
第二,如果Java使用空字符(不必使用IIRC),则它们会将其隐藏.
Second, if Java uses a null character (they don't have to IIRC) then they hide it.
碰到我的第三点,因为我不确定了.
Struck my third point because I'm not sure about it anymore.
第二我查了一下,Java字符串不是null终止的.他们依靠长度字段.
SECOND I looked it up, Java strings are not null terminated. They rely on the length field.
这篇关于"Hello" .indexOf(“")返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!