简体   繁体   English

如何在不使用 replace 方法的情况下替换 Java 中的子字符串?

[英]How would I replace a substring in Java without using the replace method?

What I need to do is replace the letter the user wants to replace in a string with X. Here's an example:我需要做的是用 X 替换用户想要在字符串中替换的字母。 这是一个例子:

replaceLetterWithX("asdfgsdfghfghj","s") replaceLetterWithX("asdfgsdfghfghj","s")

aXdfgXdfghfghj aXdfgXdfghfghj

But I can't use the replace method.但是我不能使用替换方法。 I also need to use the substring, length, and equals methods.我还需要使用 substring、length 和 equals 方法。 I'm a bit confused about where to start.我对从哪里开始有点困惑。 Here's what my code looks like now:这是我的代码现在的样子:

public static String replaceLetterWithX(String str, String c)
    {
        //This method will return 'str' with all instances of letter 'c' replaced
        //by 'X'

        String result="";
        
        int count = 0;

        //Code here

        return result;
    }

This can be done with a simple for loop using the charAt method.这可以通过使用 charAt 方法的简单 for 循环来完成。 By looping through the strings and comparing each character with the character to be replaced, we can construct the replaced string from scratch.通过遍历字符串并将每个字符与要替换的字符进行比较,我们可以从头开始构造替换字符串。 Keep in mind that this is case-sensitive, I suggest you do some research into the Java documentation to learn more about how to do that.请记住,这是区分大小写的,我建议您对 Java 文档进行一些研究,以了解有关如何执行此操作的更多信息。

public static String replaceLetterWithX(String str, String c)
    {
        //This method will return 'str' with all instances of letter 'c' replaced
        //by 'X'

        String result="";

        //Code here
        //looping through each character in the string 
        for (int i = 0; i < str.length(); i++)
        {
            //converting the character at i to string and comparing it with the given letter
            if (Character.toString(str.charAt(i)).equals(c))
            {
                result += "X";
            }
            //if it isn't add the original letter in the string
            else
            {
                result += str.charAt(i);
            }
        }
        return result;
    }

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

相关问题 java - 如何在不使用replace方法的情况下替换java中的子字符串以及该函数是否具有多个参数? - How do I replace a substring in java without using the replace method and if the function has multiple parameters? 如何在不使用replace()方法的情况下替换子字符串 - How to replace a substring without using replace() methods 不使用.replace()的替换方法 - Replace method without using .replace() 如何在Java中用引号替换子字符串? - How to replace substring with quotes in Java? 在Java中不使用replace方法替换字符串中的特定字符 - Replace specific characters in a string without using replace method in Java 如何使用java使用正则表达式替换特定的子串? - How to replace particular substring using regular expression using java? 如何使用Java使用正则表达式替换特定的子字符串 - How to replace particular substring using regular expression using java 如何在不使用字符串缓冲区类replace()方法的情况下替换字符串中的字符? - How can I replace characters in a string without using String Buffer class replace() method? 如何在不使用 replace 方法的情况下替换用户输入的字符串中的字符? - How can I replace a character in a string that the user inputs without using the replace method? 我如何用正则表达式替换此功能 - How would I replace this function with a regex replace
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM