Java 如何替换字符串中的字母?

Java 如何替换字符串中的字母?,java,Java,我应该编写代码来替换输入中的字母。例如,如果单词是“hello”,要替换的字母是“l”,加上“y”,则表示为“heyyo”。我只是不知道用户输入后该怎么办 import java.util.Scanner; public class Letter { public static void main(String[] args) { // Ask the user for 3 things: their w

我应该编写代码来替换输入中的字母。例如,如果单词是“hello”,要替换的字母是“l”,加上“y”,则表示为“heyyo”。我只是不知道用户输入后该怎么办

     import java.util.Scanner;
      public class Letter
      {
          public static void main(String[] args)
          {
         // Ask the user for 3 things: their word, letter they want to replace,
         // and replacing letter.
         Scanner input = new Scanner(System.in);
         System.out.println("Enter your word:");
         String word = input.nextLine();
         System.out.println();

         System.out.println("Enter the letter you want to replace:");
         String letter = input.nextLine();
         System.out.println();

         System.out.println("Enter the replacing letter:");
         String replace = input.nextLine();
         System.out.println();
         // Call the method replaceLetter and pass all 3 of these items to it for 
         // string processing.

     }

     // Modify this method so that it will take a third parameter from a user that is the String 
     //they 
     //want
     //to replace letterToReplace with. This method should return the modified String.
     public static int replaceLetter(String word, String letterToReplace, String replacement)
     {

         int count = 0;
         for(int i = 0; i < word.length(); i++)
         {
             if(word.substring(i, i+1).equals(letterToReplace))
             {
                 count++;
             }
         }
         return count;
     }
 }
import java.util.Scanner;
公函
{
公共静态void main(字符串[]args)
{
//向用户询问三件事:他们的单词,他们想替换的字母,
//和替换字母。
扫描仪输入=新扫描仪(System.in);
System.out.println(“输入您的单词:”);
String word=input.nextLine();
System.out.println();
System.out.println(“输入要替换的字母:”);
字符串字母=input.nextLine();
System.out.println();
System.out.println(“输入替换字母:”);
String replace=input.nextLine();
System.out.println();
//调用replaceLetter方法并将这些项中的所有3项传递给它,以便
//字符串处理。
}
//修改此方法,使其从用户处获取第三个参数,即字符串
//他们
//想要
//将letterReplace替换为。此方法应返回修改后的字符串。
公共静态int replaceLetter(字符串字、字符串字母替换、字符串替换)
{
整数计数=0;
for(int i=0;i
尝试执行下一步,如果字符与要替换的字母相同,则替换该位置的字符

   for(int i = 0; i < word.length(); i++)
   {
        if(word.charAt(i) == letterToReplace)
        {
              word = word.substring(0, i) 
              + replacement
              + word.substring(i + 1); 
              count++;
         }
   }

YCF_L是正确的,最好的方法是更换。如果出于某种原因,您需要按程序办事,这将起作用:

public static void main(String[] args)
{
    // Ask the user for 3 things: their word, letter they want to replace,
    // and replacing letter.
    Scanner input = new Scanner(System.in);
    System.out.println("Enter your word:");
    String word = input.nextLine();
    System.out.println();

    System.out.println("Enter the letter you want to replace:");
    String letter = input.nextLine();
    System.out.println();

    System.out.println("Enter the replacing letter:");
    String replace = input.nextLine();
    System.out.println();
    // Call the method replaceLetter and pass all 3 of these items to it for
    // string processing.
    System.out.println(replaceLetter(word, letter, replace));

}

// Modify this method so that it will take a third parameter from a user that is the String
//they
//want
//to replace letterToReplace with. This method should return the modified String.
public static String replaceLetter(String word, String letterToReplace, String replacement)
{
    //Short way:
    String wayOne = word.replace(letterToReplace, replacement);

    //Long way:
    String wayTwo = "";
    for(int i = 0; i < word.length(); i++){
        if(word.charAt(i) == letterToReplace.charAt(0)){
            wayTwo += replacement;
        } else {
            wayTwo += Character.toString(word.charAt(i));
        }
    }

    return wayOne + " VS " + wayTwo;
}
publicstaticvoidmain(字符串[]args)
{
//向用户询问三件事:他们的单词,他们想替换的字母,
//和替换字母。
扫描仪输入=新扫描仪(System.in);
System.out.println(“输入您的单词:”);
String word=input.nextLine();
System.out.println();
System.out.println(“输入要替换的字母:”);
字符串字母=input.nextLine();
System.out.println();
System.out.println(“输入替换字母:”);
String replace=input.nextLine();
System.out.println();
//调用replaceLetter方法并将这些项中的所有3项传递给它,以便
//字符串处理。
System.out.println(replaceLetter(word,letter,replace));
}
//修改此方法,使其从用户处获取第三个参数,即字符串
//他们
//想要
//替换字母替换为。此方法应返回修改后的字符串。
公共静态字符串替换字母(字符串字、字符串字母替换、字符串替换)
{
//短途:
字符串wayOne=word.replace(字母替换,替换);
//漫长的道路:
字符串wayTwo=“”;
for(int i=0;i
你好。替换('l','y')
@YCF_L我不能硬编码它,它必须是任何单词和任何字母。您阅读了字符串文档吗?您可以使用
返回replaceLetter.replace(letterreplace,replacement)
public static void main(String[] args)
{
    // Ask the user for 3 things: their word, letter they want to replace,
    // and replacing letter.
    Scanner input = new Scanner(System.in);
    System.out.println("Enter your word:");
    String word = input.nextLine();
    System.out.println();

    System.out.println("Enter the letter you want to replace:");
    String letter = input.nextLine();
    System.out.println();

    System.out.println("Enter the replacing letter:");
    String replace = input.nextLine();
    System.out.println();
    // Call the method replaceLetter and pass all 3 of these items to it for
    // string processing.
    System.out.println(replaceLetter(word, letter, replace));

}

// Modify this method so that it will take a third parameter from a user that is the String
//they
//want
//to replace letterToReplace with. This method should return the modified String.
public static String replaceLetter(String word, String letterToReplace, String replacement)
{
    //Short way:
    String wayOne = word.replace(letterToReplace, replacement);

    //Long way:
    String wayTwo = "";
    for(int i = 0; i < word.length(); i++){
        if(word.charAt(i) == letterToReplace.charAt(0)){
            wayTwo += replacement;
        } else {
            wayTwo += Character.toString(word.charAt(i));
        }
    }

    return wayOne + " VS " + wayTwo;
}