`

String的replace的奇怪用法

    博客分类:
  • JAVA
J# 
阅读更多
1.代码如下:
String strIds = sceneIds.toString();
System.out.println("strIds="+strIds);
int i = strIds.indexOf("[");
int j = strIds.indexOf("]");
System.out.println("i="+i+" j="+j);
if(i != -1 && j != -1){
      strIds.replace("[", "(");
      strIds.replace("]", ")");
}
System.out.println("strIds="+strIds);
大家来猜下它的结果:
结果是:
strIds=[7, 4, 8, 6, 9, 5]
i=0 j=17
strIds=[7, 4, 8, 6, 9, 5]

 

2.代码如下(和1有点不一样哦)
String strIds = sceneIds.toString();
System.out.println("strIds="+strIds);
int i = strIds.indexOf("[");
int j = strIds.indexOf("]");
System.out.println("i="+i+" j="+j);
if(i != -1 && j != -1){
      strIds = strIds.replace("[", "(");
      strIds = strIds.replace("]", ")");
}
System.out.println("strIds="+strIds);
结果如下:
strIds=[7, 4, 8, 6, 9, 5]
i=0 j=17
strIds=(7, 4, 8, 6, 9, 5)

 这就是String的replace的奇怪用法!!!!我在这里碰到了两次这样的情况,以后一定要谨记!!!!

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics