[英]Do I need to do something special to use the replace method with symbols?
Here's some code 这是一些代码
private String replaceToEncrypt(String password) {
password.replace('A','@');
password.replace('E','=');
password.replace('I','!');
password.replace('J','?');
password.replace('O','*');
password.replace('P','#');
password.replace('R','&');
password.replace('S','$');
}
Using print statements its seems that nothing happens because ABCDEFGHIJKLMNOPQRSTUVWXYZ before this method is ABCDEFGHIJKLMNOPQRSTUVWXYZ after 使用print语句似乎没有任何反应,因为此方法之前的ABCDEFGHIJKLMNOPQRSTUVWXYZ是ABCDEFGHIJKLMNOPQRSTUVWXYZ之后
Thanks 谢谢
You have to re-assign the result of each replacement, for example: 您必须重新分配每个替换的结果,例如:
password = password.replace('A','@');
This is because all Strings in Java are immutable, and any operation that modifies a String what really does is return a new String with the modifications, the original String is kept unchanged. 这是因为Java中的所有字符串都是不可变的,并且任何修改字符串实际操作的操作都会返回带有修改的新字符串,原始字符串保持不变。
replace()
according to the Java 7 API: 根据Java 7 API
replace()
:
Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.
返回一个新字符串,该字符串是使用newChar替换此字符串中所有出现的oldChar。
So in your code you need to reassign to password the new String: 因此,在您的代码中,您需要重新分配新字符串的密码:
password = password.replace('A','@');
//etc...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.