我今天午夜就要上学。除了一个问题,我几乎完成了所有任务。我需要交换" r"和" q"彼此作为价值观。所以,如果你输入" r"在编译器中你应该得到" q"如果你输入" q"你得到" r"(使用JOptionPane
)。例如,如果您的名字是Quart,编译器应该打印Ruaqt。我尝试使用replace.All方法,但一旦我只能交换" r"或" q"不是都。我知道我需要一个临时变量,但不知道其他任何事情......
我们不得不用后面的字母替换元音所以我这样做了:
String firstName = JOptionPane
.showInputDialog("What is your first name?");
String lastName = JOptionPane
.showInputDialog("What is your last name?");
String fullname = firstname + lastname;
String lowername = fullName.toLowerCase();
String encryptedname = lowername.replaceAll("a", "b")
.replaceAll("e", "f").replaceAll("i", "j").replaceAll("o", "p")
.replaceAll("u", "v");
由于
答案 0 :(得分:1)
Dunno为什么使用StringBuilder
的2个答案都会使事情变得比需要的更复杂。
以下是使用StringBuilder
进行单字符交换的方法:
public static String swapChar(String string, char c1, char c2) {
StringBuilder sb = new StringBuilder(string);
for (int i = 0; i < sb.length(); ++i) {
if (sb.charAt(i) == c1) {
sb.setCharAt(i, c2);
} else if (sb.charAt(i) == c2) {
sb.setCharAt(i, c1);
}
}
return sb.toString();
}
更新:
刚刚发现你正在寻找的东西实际上是在同时进行一堆替换。这可以通过提供Map作为参数来干净地完成:
public static String replaceChars(String string, Map<Character,Character> cmap) {
StringBuilder sb = new StringBuilder(string);
for (int i = 0; i < sb.length(); ++i) {
if (cmap.containsKey(sb.charAt(i)) {
sb.setCharAt(i, cmap.get(sb.charAt(i));
}
}
return sb.toString();
}
使用它:
// or make a util method to make these even easier to create
Map<Character,Character> cmap = new HashMap<Character,Character>();
cmap.put('r','q');
cmap.put('q','r');
cmap.put('a','b');
cmap.put('e','f');
cmap.put('i','j');
cmap.put('o','p');
cmap.put('u','v');
并简单地进行替换:
String result = replaceChars(inputString, cmap);
甚至更简单,通过使用Apache Commons Lang:
String result = StringUtils.replaceChars(inputString, "rqaeiou", "qrbfjpv");
答案 1 :(得分:0)
你可以试试这个。
private static final char Q_STR = 'q';
private static final char R_STR = 'r';
public static String replaceString(String original, int position, char strToReplace) {
StringBuilder strBuilder = new StringBuilder(original);
if (strToReplace == Q_STR) {
strBuilder.setCharAt(position, R_STR);
} else if (strToReplace == R_STR){
strBuilder.setCharAt(position, Q_STR);
}
return strBuilder.toString();
}
public static void main(String[] args) {
String firstname = "Quart";
String lastname = " QuartLastName";
String fullname = firstname + lastname;
String lowername = fullname.toLowerCase();
//get all chars in String
char[] array = lowername.toCharArray();
//list to keep original position of Q char
List<Integer> allQPosition = new ArrayList<Integer>();
//list to keep original position of R char
List<Integer> allRPosition = new ArrayList<Integer>();
for (int i = 0; i < array.length; i++) {
if(array[i] == 'q') {
allQPosition.add(i);
} else if(array[i] == 'r') {
allRPosition.add(i);
}
}
//replace q
for (Integer integer : allQPosition) {
lowername = replaceString(lowername, integer, Q_STR);
}
//replace r
for (Integer integer : allRPosition) {
lowername = replaceString(lowername, integer, R_STR);
}
//replace others
String encryptedname = lowername.replace("a", "b")
.replace("e", "f")
.replace("i", "j")
.replace("o", "p")
.replace("u", "v");
System.out.println("Result: " + encryptedname);
}
我的解决方案是:
希望这个帮助
答案 2 :(得分:-2)
public static void main(String[] args) {
String origin = "r and q";
System.out.println(newReplacement(origin, 'r', 'q'));
}
private static String newReplacement(String origin, char firstChar, char secondChar) {
StringBuffer stringBuffer = new StringBuffer(origin);
for(int i = 0; i < origin.length(); i++) {
if(origin.charAt(i) == firstChar) {
stringBuffer.replace(i, i+1, secondChar + "");
continue;
}
if(origin.charAt(i) == secondChar) {
stringBuffer.replace(i, i+1, firstChar + "");
}
}
return stringBuffer.toString();
}
用简单的方法重写替换方法。