[英]Replace a certain character from a String
In this code, I am trying to replace a letter from a String entered by a user.在这段代码中,我试图替换用户输入的字符串中的一个字母。 For example, the program asks for a word, and the user enters "hello", the next variable that the user inputs is the letter that is going to be replaced ("l"), and the final variable is the letter that is going to replace the previous variable ("x").
例如,程序要求输入一个单词,用户输入“hello”,用户输入的下一个变量是要替换的字母(“l”),最后一个变量是要替换的字母替换之前的变量(“x”)。
The result should be "hexxo", but my program is only replacing one of the l's, what is wrong with my program?结果应该是“hexxo”,但我的程序只是替换了其中一个 l,我的程序有什么问题?
import java.util.Scanner;
public class Letter
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter your word:");
String word = input.nextLine();
System.out.println("\nEnter the letter you want to replace:");
String replace = input.nextLine();
System.out.println("\nEnter the replacing letter:");
String add = input.nextLine();
System.out.println(replaceLetter(word, replace, add));
}
public static String replaceLetter(String word, String letterToReplace, String add)
{
String newWord = "";
for(int i = 0; i < word.length(); i++)
{
if(word.substring(i, i+1).equals(letterToReplace))
{
String front = word.substring(0, word.indexOf(letterToReplace));
String back = word.substring(word.indexOf(letterToReplace)+1);
newWord = front + add + back;
}
}
return newWord;
}
}
There are 2 problems.有2个问题。
First, your program updates newWord
everytime,首先,您的程序每次都会更新
newWord
,
but whenever newWord
is updated, it is calculated with not-updated word
.但是每当更新
newWord
时,都会使用未更新的word
进行计算。
if(word.substring(i, i+1).equals(letterToReplace))
{
String front = !!!!!word!!!!!.substring(0, word.indexOf(letterToReplace));
String back = !!!!!word!!!!!.substring(word.indexOf(letterToReplace)+1);
newWord = front + add + back;
}
Second, why substring()
in the if block does not use i
?其次,为什么 if 块中的
substring()
不使用i
?
String front = word.substring(0, !!!!!word.indexOf(letterToReplace)!!!!!);
String back = word.substring(!!!!!word.indexOf(letterToReplace)+1!!!!!);
The reason why your algorithm is not working, is that you're assigning a new String to newWord
every time you replace a letter.您的算法不起作用的原因是您每次替换字母时都会为
newWord
分配一个新字符串。 So you will only get an output of the last replacement.所以你只会得到最后一个替换的 output。
Every time the loop checks the the original string word
.每次循环检查原始字符串
word
。
First, your program updates newWord
every-time, but whenever newWord
is updated, it is calculated with not-updated word
.首先,您的程序每次都会更新
newWord
,但是每当更新newWord
时,都会使用未更新的word
进行计算。
Just remove the variable newWord
and perform operations on variable word
only as presented in the following code:只需删除变量
newWord
并仅对变量word
执行操作,如下面的代码所示:
import java.util.Scanner;
public class Letter
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter your word:");
String word = input.nextLine();
System.out.println("\nEnter the letter you want to replace:");
String replace = input.nextLine();
System.out.println("\nEnter the replacing letter:");
String add = input.nextLine();
System.out.println(replaceLetter(word, replace, add));
}
public static String replaceLetter(String word, String letterToReplace, String add)
{
for(int i = 0; i < word.length(); i++)
{
if(word.substring(i, i+1).equals(letterToReplace))
{
String front = word.substring(0, word.indexOf(letterToReplace));
String back = word.substring(word.indexOf(letterToReplace)+1);
word = front + add + back;
}
}
return word;
}
}
You could simply use String#replace
or String#replaceAll
.您可以简单地使用
String#replace
或String#replaceAll
。 Just take a look into the java-doc to understand how they work.只需查看 java-doc 即可了解它们的工作原理。
Because you are using word.indexOf(letterToReplace)
which will always return the first index of string.因为您正在使用
word.indexOf(letterToReplace)
它将始终返回字符串的第一个索引。 So, Instead, you need to use word.indexOf(letterToReplace,i);
所以,相反,你需要使用
word.indexOf(letterToReplace,i);
with minor code, changes will help you to solve your problem.使用少量代码,更改将帮助您解决问题。 Try it yourself.
自己试试。
Happy Coding !!!快乐编码!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.