java字符串多个?替换
时间: 2024-11-27 12:13:03 浏览: 23
在Java中,如果你想要替换字符串中的多个特定字符或子串,你可以使用`replace()`、`replaceAll()`或正则表达式结合`Pattern`和`Matcher`类。这里以`replaceAll()`为例,它接受一个模式作为参数,并返回一个新的字符串,其中所有匹配该模式的部分都被替换成指定的替换字符串。
例如:
```java
String original = "Hello, world!";
String replacement = "Java";
String result = original.replaceAll("o", replacement); // 将所有的 'o' 替换为 'Java'
System.out.println(result); // 输出 "Hell Java, wJrld!"
```
如果你想替换多个特定字符,可以多次调用`replaceAll()`函数:
```java
String result2 = result.replaceAll(",", ""); // 移除逗号
System.out.println(result2); // 输出 "HellJava wJrld!"
```
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![text/x-c](https://img-home.csdnimg.cn/images/20250102104920.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)