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

后端 未结 4 735
孤独总比滥情好
孤独总比滥情好 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:16

    A simplistic approach would be:

    String phrase = "I want to replace letters in this phase";
    phrase = phrase.replace(letter.toLowerCase(), exchange);
    phrase = phrase.replace(letter.toUpperCase(), exchange);
    

    EDIT: Added toLowerCase() as per suggestion below.

提交回复
热议问题