好的,所以我想替换一个具有特定输入的用户返回的字符串。我想用'?'替换字符串中的每个小写字母,但不替换空格!插入'?'的地方应该在print语句中,最后是随机空格(“”)。谢谢您的帮助。
import java.util.Scanner;
public class Program05 {
public static void main(String[] args)
{
Scanner scanner01 = new Scanner(System.in);
String inputPhrase;
do {
System.out.print("Enter a common phrase to begin!: ");
inputPhrase = scanner01.nextLine();
} while (!inputPhrase.matches("[a-z ]+"));
System.out.print("Press [ENTER] to continue...");
try
{
System.in.read();
System.in.skip(System.in.available());
} catch(Exception e){e.printStackTrace();} //though that was a neat little touch
do {
boolean valid = true;
for (int i = 0; i < inputPhrase.length(); i++)
{
System.out.println("Common Phrase\n-------------" + "\n" + " " );
}
//System.out.print(inputPhrase);
} while (!inputPhrase.matches("[a-z ]+"));
}
}
答案 0 :(得分:0)
假设您的String在变量s中。然后你可以用
循环逐字逐句for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
}
所以这归结为检查char c是否为小写,并采取相应的行动。确定char c是否为小写的一种可能方法是检查其int值并查看其值是否介于97和122之间。
int intValue = (int) c;
if (intValue >= 97 && intValue <= 122) { }
如果是,请将其替换为问号。
一种简单的方法是创建一个临时字符串,当你遍历源字符串时,添加每个字母,直到你得到小写字母,在这种情况下你添加一个问号,直到你完成。
答案 1 :(得分:0)
您可以简单地使用API中内置的正则表达式功能,例如......
String question = "This is a Test String".replaceAll("([a-z])", "?");
System.out.println(question);
输出......
T??? ?? ? T??? S?????
nb [a-z]
给出相同的结果
答案 2 :(得分:0)
您可以将replaceAll
与正则表达式\p{Lower}
结合使用:
String str = "Hello World";
System.out.println(str.replaceAll("\\p{Lower}", "?"));
H???? W????
答案 3 :(得分:0)
在大多数情况下,我们希望隐藏我们的个人详细信息,例如电话号码,电子邮件等。在这里,我假设您要隐藏电子邮件或电话号码中的字符。
假设CredentialModel
是一个看起来像这样的类:
public class CredentialModel {
private String username;
private String phoneno;
private String email;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPhoneno() {
return phoneno;
}
public void setPhoneno(String phoneno) {
this.phoneno = phoneno;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "CredentialModel [username=" + username + ", phoneno=" + phoneno + ", email=" + email + "]";
}
}
和加密方法如下:
public CredentialModel encryptPrivateDetails(CredentialModel credentialModel) {
String phoneno = credentialModel.getPhoneno();
String email = credentialModel.getEmail().split("\\@")[0];
String domain = credentialModel.getEmail().split("\\@")[1];
if (phoneno != null) {
int minlength = 9;
int blocklength = 5;
if (phoneno.length() > blocklength) {
if (phoneno.length() > minlength) {
int visibleStartLength = 3;
credentialModel.setPhoneno(phoneno.substring(0, visibleStartLength)
+ String.format("%" + blocklength + "s", "").replace(' ', 'x')
+ phoneno.substring(visibleStartLength + blocklength));
} else
credentialModel.setPhoneno(String.format("%" + blocklength + "s", "").replace(' ', 'x')
+ phoneno.substring(blocklength));
}
}
if (email != null) {
int minlength = 2;
if (email.length() > minlength)
credentialModel.setEmail(email.substring(0, minlength)
+ String.format("%" + (email.length() - minlength + 1) + "s", "@").replace(' ', 'x') + domain);
else
credentialModel.setEmail(String.format("%" + (email.length() + 1) + "s", "@").replace(' ', 'x') + domain);
}
return credentialModel;
}
请根据您的要求进行调整。