简体   繁体   English

如何递归使用 replaceAll() function?

[英]How do I use the replaceAll() function recursively?

The program should allow a user to enter a string, a substring they wish to find and another string with which they wish to replace the found substring.该程序应该允许用户输入一个字符串,一个他们希望找到的 substring 和另一个他们希望用它替换找到的 substring 的字符串。

The output of your program should be similar to the output given below:您的程序的 output 应该类似于下面给出的 output:

Please enter a string: Hello world请输入字符串:Hello world

Please enter the substring you wish to find: llo请输入您要查找的substring:llo

Please enter a string to replace the given substring: @@请输入一个字符串来替换给定的 substring:@@

Your new string is: he@@ world你的新字符串是:he@@ world

I am new to Java and cant find and so far this is what I have:我是 Java 的新手,找不到,到目前为止,这就是我所拥有的:

import java.util.Scanner;

public class searchReplace 
{
    static String word, substring, newWord;
    static String output = "";
    

    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);
        
        System.out.println("Please enter a string: ");
        word = input.next();
        
        System.out.println("Please enter the substring you wish to find: ");
        substring = input.next();
        
        System.out.println("Please enter a string to replace the given substring: ");
        newWord = input.next();
        
        replace(substring,newWord);
        
        
        
        input.close();


    }


    private static void replace(String substring, String newWord) 
    {
        if(output.contains(newWord))
        {
            System.out.println(output);
        }
        
        else
        {
            output = word.replaceAll("substring","newWord");
            replace(substring,newWord);
        }
        
    }

}

I Get The Error:我得到错误:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.contains(java.lang.CharSequence)" because "searchReplace.output" is null at searchReplace.replace(searchReplace.java:33) at searchReplace.main(searchReplace.java:21) Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.contains(java.lang.CharSequence)" because "searchReplace.output" is null at searchReplace.replace(searchReplace.java:33) at searchReplace.main(搜索替换.java:21)

For your goal, you need to use just:为了您的目标,您只需要使用:

output = word.replace(substring, newWord);

instead of:代替:

word.replaceAll("substring", "newWord");

You don't need to make any recursion.您不需要进行任何递归。 Replace function will replace your substring inside word with the newWord for each occurence.替换 function 将用每个出现的 newWord 替换您的 ZE83AED3DDF4667DEC0DAAAACB2BB3BE0BZ 内部单词。

Recursion is not needed:不需要递归:

public static void main(String [] args) {
    try (Scanner scanner = new Scanner(System.in)) {
        System.out.println("Enter String: ");
        String input = scanner.nextLine();
        System.out.println("String to replace: ");
        String toReplace = scanner.nextLine();
        System.out.println("Replace with: ");
        String replaceWith = scanner.nextLine();
        
        System.out.println(input.replaceAll(toReplace, replaceWith));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM