java中string的replace方法
时间: 2023-04-27 18:00:22 浏览: 69
Java中的String类有一个replace方法,可以用来替换字符串中的某些字符或子字符串。
语法:
```
public String replace(char oldChar, char newChar)
public String replace(CharSequence target, CharSequence replacement)
```
第一个参数是需要被替换的字符或子字符串,第二个参数是新的字符或子字符串。
例如:
```
String str = "Hello World";
str = str.replace("World", "Java");
System.out.println(str); // "Hello Java"
```
注意:replace方法会创建一个新的字符串,原字符串不会被改变。
相关问题
java stringreplace
好的,针对您的问题 "java stringreplace",我可以为您提供以下回答:
在 Java 中,可以使用 String 类的 replace() 方法来替换字符串中的某些字符或字符串。该方法的语法如下:
```
public String replace(char oldChar, char newChar)
public String replace(CharSequence target, CharSequence replacement)
```
其中,第一个方法将字符串中所有的 oldChar 字符替换为 newChar 字符,第二个方法将字符串中所有的 target 字符串替换为 replacement 字符串。
例如,下面的代码将字符串中所有的 "a" 替换为 "b":
```
String str = "java stringreplace";
String newStr = str.replace("a", "b");
System.out.println(newStr); // 输出 "jbvb stringreplbce"
```
java String类replace方法源码分析
好的,关于Java String类的replace方法,其源码如下:
```
public String replace(char oldChar, char newChar) {
if (oldChar != newChar) {
int len = value.length;
int i = -1;
char[] val = value; /* avoid getfield opcode */
while (++i < len) {
if (val[i] == oldChar) {
break;
}
}
if (i < len) {
char buf[] = new char[len];
for (int j = 0; j < i; j++) {
buf[j] = val[j];
}
while (i < len) {
char c = val[i];
buf[i] = (c == oldChar) ? newChar : c;
i++;
}
return new String(buf, true);
}
}
return this;
}
```
这个方法用于将字符串中的一个字符替换成另一个字符,并返回新的字符串。我们来逐行分析一下它的源码:
1. 如果要替换的字符和替换后的字符相同,则直接返回原字符串。
```
if (oldChar != newChar) {
```
2. 获取字符串的长度和字符数组。
```
int len = value.length;
char[] val = value; /* avoid getfield opcode */
```
3. 在字符数组中查找要替换的字符。
```
int i = -1;
while (++i < len) {
if (val[i] == oldChar) {
break;
}
}
```
4. 如果找到了要替换的字符,则将其替换为新字符,否则直接返回原字符串。
```
if (i < len) {
char buf[] = new char[len];
for (int j = 0; j < i; j++) {
buf[j] = val[j];
}
while (i < len) {
char c = val[i];
buf[i] = (c == oldChar) ? newChar : c;
i++;
}
return new String(buf, true);
}
```
5. 如果要替换的字符不存在,则直接返回原字符串。
```
return this;
```
总体来说,这个方法的实现比较简单,就是遍历字符数组,找到要替换的字符,然后再将其替换成新字符,最后返回新的字符串。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)