英文:
Replace character in a String with HashMap
问题
我有一些想法来做这个
我在if语句部分卡住了,怎么样才能使比较起作用呢?
这个示例的预期输出是:字符串"ones"用于测试映射"numberthree"。
String s = "The string 1s is use 2 test the map number3.";
HashMap<Integer, String> map = new HashMap<>();
map.put(1, "one");
map.put(2, "two");
map.put(3, "three");
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) 等于 map.contains(1)) {
s[i] = map.get(1);
}
}
英文:
I have some idea to do this
I'm stuck on the if statement part, how can I make the comparison work?
The expected output for this example is: The string ones is use two test the map numberthree.
String s = "The string 1s is use 2 test the map number3.";
HashMap<Integer,String> map = new HashMap<>();
map.put(1,"one");
map.put(2,"two");
map.put(3,"three");
for (int i = 0; i < s.length(); i++){
if (s.charAt(i) is equals to map.contains(1){
s[i] = map.get(1);
}
}
答案1
得分: 2
你需要将字符转换为数值以检查是否为 HashMap
中的 key
。更好的方法是使用 StringBuilder
,因为在找到它在映射中时,你正在追加 String
:
String s = "The string 1s is use 2 test the map number3.";
HashMap<Integer, String> map = new HashMap<>();
map.put(1, "one");
map.put(2, "two");
map.put(3, "three");
StringBuilder sb = new StringBuilder();
for (char c : s.toCharArray()) {
if (Character.isDigit(c) && map.containsKey(Character.getNumericValue(c))) {
sb.append(map.get(Character.getNumericValue(c)));
} else {
sb.append(c);
}
}
System.out.println(sb.toString());
另一种解决方案是将字符存储为 key
:
String s = "The string 1s is use 2 test the map number3.";
HashMap<Character, String> map = new HashMap<>();
map.put('1', "one");
map.put('2', "two");
map.put('3', "three");
StringBuilder sb = new StringBuilder();
for (char c : s.toCharArray()) {
if (map.containsKey(c)) {
sb.append(map.get(c));
} else {
sb.append(c);
}
}
System.out.println(sb.toString());
输出:
The string ones is use two test the map numberthree.
英文:
You need to convert the character to the numeric value to check if it's a key
in the HashMap
. A better approach would be to use a StringBuilder
since you are appending String
in case it's found in the map:
String s = "The string 1s is use 2 test the map number3.";
HashMap<Integer,String> map = new HashMap<>();
map.put(1,"one");
map.put(2,"two");
map.put(3,"three");
StringBuilder sb = new StringBuilder();
for (char c : s.toCharArray()){
if (Character.isDigit(c) && map.containsKey(Character.getNumericValue(c))){
sb.append(map.get(Character.getNumericValue(c)));
}else {
sb.append(c);
}
}
System.out.println(sb.toString());
Another solution would be to store characters as the key
:
String s = "The string 1s is use 2 test the map number3.";
HashMap<Character,String> map = new HashMap<>();
map.put('1',"one");
map.put('2',"two");
map.put('3',"three");
StringBuilder sb = new StringBuilder();
for (char c : s.toCharArray()){
if (map.containsKey(c)){
sb.append(map.get(c));
}else {
sb.append(c);
}
}
System.out.println(sb.toString());
Output:
The string ones is use two test the map numberthree.
答案2
得分: 1
你可以迭代映射并将映射键用其值替换为字符串。
for (Map.Entry<Integer, String> e : map.entrySet()) {
s = s.replace(e.getKey().toString(), e.getValue());
}
我建议你不要按字符在字符串中进行迭代,因为如果你想要替换像22这样的两位或更多位数,你会遇到问题。
示例:
String s = "The string 1s is use 2 test the map number3.";
HashMap<Integer, String> map = new HashMap<>();
map.put(1, "one");
map.put(2, "two");
map.put(3, "three");
for (Map.Entry<Integer, String> e : map.entrySet()) {
s = s.replace(e.getKey().toString(), e.getValue());
}
System.out.println(s);
输出:
The string ones is use two test the map numberthree.
英文:
You can iterate the map and replace map key by its value in String.
for (Map.Entry<Integer,String> e: map.entrySet()) {
s = s.replace(e.getKey().toString(), e.getValue());
}
I recommend you not iterate by character in string since if you want to replace two or more digit numbers like 22 you faced problem.
Demo:
String s = "The string 1s is use 2 test the map number3.";
HashMap<Integer,String> map = new HashMap<>();
map.put(1,"one");
map.put(2,"two");
map.put(3,"three");
for (Map.Entry<Integer,String> e: map.entrySet()) {
s = s.replace(e.getKey().toString(), e.getValue());
}
System.out.println(s);
Output:
The string ones is use two test the map numberthree.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论