java indexof(int ch)不解,忘高手解惑。

chenlin860209 2011-11-22 07:42:27
偶然看到一段源码(FreeMarker 中的),去除部分其他代码,主要类似代码如下:



String name = "http://www.csdn.net";

int zIdx = name.indexOf(0);

if (zIdx != -1)
return null;



我试了很多字符串作为name的值,zIdx 值都为-1,
很想知道在什么情况下zIdx 值不等于-1, 再有indexof(0) 的作用究竟是什么?

查看了indexof方法的注解,可惜英文不是很好,不能理解它的意思,忘高手能解惑!感谢~~
...全文
410 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenlin860209 2011-11-23
  • 打赏
  • 举报
回复

int a = '0';
char b = 0;
char c = '\u0000';
int d = ' ';
Object e = null;

System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
System.out.println(e);

String name = b+"www.csdn.net";

int zIdx = name.indexOf(0);
System.out.println(zIdx);

if (zIdx != -1)
System.out.println("成立!");



char 0 就是上面代码中 b 和 c的值,打印输出是空,却又不等于“ ”,估计是我们不可见的一些特殊字符

到目前还是不能理解 Freemarker中为什么需要那样一个判断,难道 '\u0000' 在字符串处理中会出现bug? 不解,罢了,结贴。

感谢逸飞的解答,至少帮我理解了char,不然 我一定会认为 int a = 'a'; 是错语句,哈哈

也感谢脉动的关注,我英文也不好,哎,得练。
梅小西Echo 2011-11-23
  • 打赏
  • 举报
回复

//打印出来看看呗
System.out.println((char)0);
脉动 2011-11-23
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 huntor 的回复:]
这个 indexOf(0) 应该是 c字符串的结尾NULL。
http://stackoverflow.com/questions/318775/null-u0000-in-java-string
[/Quote]
英语不好, 就认为是:c字符串的结尾NULL。
脉动 2011-11-23
  • 打赏
  • 举报
回复
public int indexOf(int ch)
返回指定字符在此字符串中第一次出现处的索引。如果在此 String 对象表示的字符序列中出现值为 ch 的字符,则返回第一次出现该字符的索引;如果此字符串中没有这样的字符,则返回 -1。

0对应的字符是什么?我也好奇一下,等高手解答,

if (zIdx != -1 && zIdx < cppIdx) return null;
//当字符串中包含该字符(0对应的字符),且该字符出现的位置在字符串“:\\”之后,返回null
huntor 2011-11-23
  • 打赏
  • 举报
回复
这个 indexOf(0) 应该是 c字符串的结尾NULL。
http://stackoverflow.com/questions/318775/null-u0000-in-java-string
chenlin860209 2011-11-23
  • 打赏
  • 举报
回复
我大致明白char的意思了 感谢逸飞专家的指导,但我还是有一个问题不解,就是String.indexOf(0)究竟定位是哪个字符

char a = 0; 是这个a的值么?

我贴出FreeMarker 的 TemplateCache的部分源码,我就是有点不解为什么需要这样子一个判断


private static String normalizeName(String name) {
int cppIdx = name.indexOf("://");
if (cppIdx > 0) {
int zIdx = name.indexOf(0);
if (zIdx != -1 && zIdx < cppIdx) return null;
return name;
}

for(;;) {
int parentDirPathLoc = name.indexOf(PARENT_DIR_PATH);
if(parentDirPathLoc == 0) {
// If it starts with /../, then it reaches outside the template
// root.
return null;
}
if(parentDirPathLoc == -1) {
if(name.startsWith(PARENT_DIR_PATH_PREFIX)) {
// Another attempt to reach out of template root.
return null;
}
break;
}
int previousSlashLoc = name.lastIndexOf(SLASH, parentDirPathLoc - 1);
name = name.substring(0, previousSlashLoc + 1) +
name.substring(parentDirPathLoc + PARENT_DIR_PATH.length());
}
for(;;) {
int currentDirPathLoc = name.indexOf(CURRENT_DIR_PATH);
if(currentDirPathLoc == -1) {
if(name.startsWith(CURRENT_DIR_PATH_PREFIX)) {
name = name.substring(CURRENT_DIR_PATH_PREFIX.length());
}
break;
}
name = name.substring(0, currentDirPathLoc) +
name.substring(currentDirPathLoc + CURRENT_DIR_PATH.length() - 1);
}
// Editing can leave us with a leading slash; strip it.
if(name.length() > 1 && name.charAt(0) == SLASH) {
name = name.substring(1);
}
return name;
}



其中上面部分是针对WEB类型的地址 进行格式化一样的操作,我主要就是不解这部分,在什么时候返回null

下面是针对相对地址的部分,可以不看,我用红色标记出主要代码

int cppIdx = name.indexOf("://");
if (cppIdx > 0) {
int zIdx = name.indexOf(0);
if (zIdx != -1 && zIdx < cppIdx) return null;
return name;
}


专家能再帮我解惑一下啊? 何时返回null值 就是name为什么值时才会这样发生?

我想这个判断应该是有意义的,虽然我不太明白。
huntor 2011-11-22
  • 打赏
  • 举报
回复
Returns the index within this string of the first occurrence of the specified character.

char 本质上是一个16bit的int

        int a = 'a';
没错
chenlin860209 2011-11-22
  • 打赏
  • 举报
回复
indexof 有一个方法是这样的


int indexof(int ch)

int型,不是char型或者String型
huntor 2011-11-22
  • 打赏
  • 举报
回复
indexOf(char c)
indexOf(String s)

参数第一次出现的位置。

name.indexOf('h') 0
name.indexOf("t") 1

51,408

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧