java输入一个字符串,然后用另外一个字符串全部替换输入字符串中的某一字符
时间: 2023-04-10 11:04:59 浏览: 120
可以使用 String 类的 replaceAll() 方法来实现这个功能,具体代码如下:
String input = "Hello World";
String replaceStr = "l";
String replaceWith = "x";
String output = input.replaceAll(replaceStr, replaceWith);
System.out.println(output);
这段代码会将 input 字符串中所有的 "l" 字符替换为 "x",输出结果为 "Hexxo Worxd"。
相关问题
输入一个字符串,然后用另外一个字符串全部替换输入字符串中的某一字符java
可以使用 String 类的 replaceAll() 方法来实现这个功能,具体代码如下:
String input = "hello world";
String replaceStr = "o";
String replaceWith = "a";
String output = input.replaceAll(replaceStr, replaceWith);
这段代码将会把 input 字符串中所有的 "o" 字符替换成 "a",最终输出的结果是 "hella warld"。
java输入一个字符串,若字符串中出现邪恶,则把它替换为**
好的,可以使用Java中的replace方法来实现字符串替换。可以尝试以下代码:
```
import java.util.Scanner;
public class ReplaceEvil {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入一个字符串:");
String str = scanner.nextLine();
String replacedStr = str.replace("邪恶", "**");
System.out.println("替换后的字符串为:" + replacedStr);
}
}
```
这段代码会先提示用户输入一个字符串,然后使用replace方法将其中的“邪恶”替换为“**”,最后输出替换后的字符串。
相关推荐
![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_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)
![](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)