簡體   English   中英

替換字符串中的某個字符

[英]Replace a certain character from a String

在這段代碼中,我試圖替換用戶輸入的字符串中的一個字母。 例如,程序要求輸入一個單詞,用戶輸入“hello”,用戶輸入的下一個變量是要替換的字母(“l”),最后一個變量是要替換的字母替換之前的變量(“x”)。

結果應該是“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;
        
        
    }
}

有2個問題。

首先,您的程序每次都會更新newWord
但是每當更新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;
}

其次,為什么 if 塊中的substring()不使用i

String front = word.substring(0, !!!!!word.indexOf(letterToReplace)!!!!!);
String back = word.substring(!!!!!word.indexOf(letterToReplace)+1!!!!!);

您的算法不起作用的原因是您每次替換字母時都會為newWord分配一個新字符串。 所以你只會得到最后一個替換的 output。

每次循環檢查原始字符串word

首先,您的程序每次都會更新newWord ,但是每當更新newWord時,都會使用未更新的word進行計算。

只需刪除變量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;
        
        
    }
}

您可以簡單地使用String#replaceString#replaceAll 只需查看 java-doc 即可了解它們的工作原理。

因為您正在使用word.indexOf(letterToReplace)它將始終返回字符串的第一個索引。 所以,相反,你需要使用word.indexOf(letterToReplace,i); 使用少量代碼,更改將幫助您解決問題。 自己試試。

快樂編碼!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM