---
title: "String的replace的奇怪用法"
author: "duyunfengsolo"
date: "2009-11-05T18:03:39Z"
tags: []
slug: "string-replace"
draft: false
lastmod: "2009-11-05T18:03:39Z"
id: 510837
url: "https://solodu.iteye.com/blog/510837"
---

```
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的奇怪用法！！！！我在这里碰到了两次这样的情况，以后一定要谨记！！！！