用HashMap替换字符串中的字符

huangapple go评论97阅读模式
英文:

Replace character in a String with HashMap

问题

  1. 我有一些想法来做这个
  2. 我在if语句部分卡住了怎么样才能使比较起作用呢
  3. 这个示例的预期输出是字符串"ones"用于测试映射"numberthree"
  4. String s = "The string 1s is use 2 test the map number3.";
  5. HashMap<Integer, String> map = new HashMap<>();
  6. map.put(1, "one");
  7. map.put(2, "two");
  8. map.put(3, "three");
  9. for (int i = 0; i < s.length(); i++) {
  10. if (s.charAt(i) 等于 map.contains(1)) {
  11. s[i] = map.get(1);
  12. }
  13. }
英文:

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.

  1. String s = &quot;The string 1s is use 2 test the map number3.&quot;;
  2. HashMap&lt;Integer,String&gt; map = new HashMap&lt;&gt;();
  3. map.put(1,&quot;one&quot;);
  4. map.put(2,&quot;two&quot;);
  5. map.put(3,&quot;three&quot;);
  6. for (int i = 0; i &lt; s.length(); i++){
  7. if (s.charAt(i) is equals to map.contains(1){
  8. s[i] = map.get(1);
  9. }
  10. }

答案1

得分: 2

你需要将字符转换为数值以检查是否为 HashMap 中的 key。更好的方法是使用 StringBuilder,因为在找到它在映射中时,你正在追加 String

  1. String s = "The string 1s is use 2 test the map number3.";
  2. HashMap<Integer, String> map = new HashMap<>();
  3. map.put(1, "one");
  4. map.put(2, "two");
  5. map.put(3, "three");
  6. StringBuilder sb = new StringBuilder();
  7. for (char c : s.toCharArray()) {
  8. if (Character.isDigit(c) && map.containsKey(Character.getNumericValue(c))) {
  9. sb.append(map.get(Character.getNumericValue(c)));
  10. } else {
  11. sb.append(c);
  12. }
  13. }
  14. System.out.println(sb.toString());

另一种解决方案是将字符存储为 key

  1. String s = "The string 1s is use 2 test the map number3.";
  2. HashMap<Character, String> map = new HashMap<>();
  3. map.put('1', "one");
  4. map.put('2', "two");
  5. map.put('3', "three");
  6. StringBuilder sb = new StringBuilder();
  7. for (char c : s.toCharArray()) {
  8. if (map.containsKey(c)) {
  9. sb.append(map.get(c));
  10. } else {
  11. sb.append(c);
  12. }
  13. }
  14. System.out.println(sb.toString());

输出:

  1. 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:

  1. String s = &quot;The string 1s is use 2 test the map number3.&quot;;
  2. HashMap&lt;Integer,String&gt; map = new HashMap&lt;&gt;();
  3. map.put(1,&quot;one&quot;);
  4. map.put(2,&quot;two&quot;);
  5. map.put(3,&quot;three&quot;);
  6. StringBuilder sb = new StringBuilder();
  7. for (char c : s.toCharArray()){
  8. if (Character.isDigit(c) &amp;&amp; map.containsKey(Character.getNumericValue(c))){
  9. sb.append(map.get(Character.getNumericValue(c)));
  10. }else {
  11. sb.append(c);
  12. }
  13. }
  14. System.out.println(sb.toString());

Another solution would be to store characters as the key:

  1. String s = &quot;The string 1s is use 2 test the map number3.&quot;;
  2. HashMap&lt;Character,String&gt; map = new HashMap&lt;&gt;();
  3. map.put(&#39;1&#39;,&quot;one&quot;);
  4. map.put(&#39;2&#39;,&quot;two&quot;);
  5. map.put(&#39;3&#39;,&quot;three&quot;);
  6. StringBuilder sb = new StringBuilder();
  7. for (char c : s.toCharArray()){
  8. if (map.containsKey(c)){
  9. sb.append(map.get(c));
  10. }else {
  11. sb.append(c);
  12. }
  13. }
  14. System.out.println(sb.toString());

Output:

  1. The string ones is use two test the map numberthree.

答案2

得分: 1

你可以迭代映射并将映射键用其值替换为字符串。

  1. for (Map.Entry<Integer, String> e : map.entrySet()) {
  2. s = s.replace(e.getKey().toString(), e.getValue());
  3. }

我建议你不要按字符在字符串中进行迭代,因为如果你想要替换像22这样的两位或更多位数,你会遇到问题。

示例:

  1. String s = "The string 1s is use 2 test the map number3.";
  2. HashMap<Integer, String> map = new HashMap<>();
  3. map.put(1, "one");
  4. map.put(2, "two");
  5. map.put(3, "three");
  6. for (Map.Entry<Integer, String> e : map.entrySet()) {
  7. s = s.replace(e.getKey().toString(), e.getValue());
  8. }
  9. System.out.println(s);

输出:

  1. The string ones is use two test the map numberthree.
英文:

You can iterate the map and replace map key by its value in String.

  1. for (Map.Entry&lt;Integer,String&gt; e: map.entrySet()) {
  2. s = s.replace(e.getKey().toString(), e.getValue());
  3. }

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:

  1. String s = &quot;The string 1s is use 2 test the map number3.&quot;;
  2. HashMap&lt;Integer,String&gt; map = new HashMap&lt;&gt;();
  3. map.put(1,&quot;one&quot;);
  4. map.put(2,&quot;two&quot;);
  5. map.put(3,&quot;three&quot;);
  6. for (Map.Entry&lt;Integer,String&gt; e: map.entrySet()) {
  7. s = s.replace(e.getKey().toString(), e.getValue());
  8. }
  9. System.out.println(s);

Output:

  1. The string ones is use two test the map numberthree.

huangapple
  • 本文由 发表于 2020年9月20日 18:54:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63978184.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定