如果用户输入随机字母,如何更改给定字符串中的所有字母? [英] If user inputs random letter, how do I change all of that letter in a given string?

查看:107
本文介绍了如果用户输入随机字母,如何更改给定字符串中的所有字母?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何使用字符包装来根据用户输入改变字符串。如果字符串是'鲍勃喜欢建立建筑'而用户输入'b'我必须更改小写和大写字母bs的输出。

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 make the out put change both the lower case and upper case letter bs.

这它必须添加到:

 System.out.print("\nWhat character would you like to replace?");
 String letter = input.nextLine();
 System.out.print("What character would you like to replace "+letter+" with?");
 String exchange = input.nextLine();


推荐答案

怎么样:

myString = myString.replace(letter,exchange);

编辑:
myString是你要替换字母的字符串。

myString is the string you want to replace the letter in.

信件取自您的代码,这是要替换的信件。

letter is taken from your code, it is the letter to be replaced.

交换也取自您的代码,它是字母要替换的字符串。

exchange is also taken from your code, it is the string that letter is to be replace with.

当然,对于大写字母和小写字母,您需要再次执行此操作,因此它将是:

Of course you would need to do this again for the upper case letter and lower case so it would be:

myString = myString.replace(letter.toLowerCase(),exchange);
myString = myString.replace(letter.toUpperCase(),exchange);

为了涵盖输入的字母为大写或小写的情况。

In order to cover the case where the entered letter is either lower or uppercase.

这篇关于如果用户输入随机字母,如何更改给定字符串中的所有字母?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆