If user inputs random letter, how do I change all of that letter in a given string?

后端 未结 4 803
孤独总比滥情好
孤独总比滥情好 2021-01-29 16:27

I am trying to figure out how to use character wrapping to mutate a string based on user input. If string is \'Bob loves to build building\' and user enters \'b\' I have to mak

4条回答
  •  你的背包
    2021-01-29 17:22

    Check replace method:

    public String replace(char oldChar, char newChar)
    

    Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.

    For more details see [String#replace](http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#replace(char, char))

    EDIT:

    class ReplaceDemo
    {
        public static void main(String[] args)
        {
            String inputString = "It is that, that's it.";
            Char replaceMe = 'i';
            Char replaceWith = 't';
    
            String newString = inputString.Replace(replaceMe.toUpperCase(), replaceWith);
            newString = newString.Replace(replaceMe.toLowerCase(), replaceWith);
        }
    }
    

    Does that solve your problem?

提交回复
热议问题