用HashMap替换字符串中的字符

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

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 = &quot;The string 1s is use 2 test the map number3.&quot;;
 
        HashMap&lt;Integer,String&gt; map = new HashMap&lt;&gt;();
        map.put(1,&quot;one&quot;);
        map.put(2,&quot;two&quot;);
        map.put(3,&quot;three&quot;);
  
        for (int i = 0; i &lt; 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 = &quot;The string 1s is use 2 test the map number3.&quot;;
HashMap&lt;Integer,String&gt; map = new HashMap&lt;&gt;();
map.put(1,&quot;one&quot;);
map.put(2,&quot;two&quot;);
map.put(3,&quot;three&quot;);
		
StringBuilder sb = new StringBuilder();
for (char c : s.toCharArray()){
	if (Character.isDigit(c) &amp;&amp; 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 = &quot;The string 1s is use 2 test the map number3.&quot;;
HashMap&lt;Character,String&gt; map = new HashMap&lt;&gt;();
map.put(&#39;1&#39;,&quot;one&quot;);
map.put(&#39;2&#39;,&quot;two&quot;);
map.put(&#39;3&#39;,&quot;three&quot;);
		        
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&lt;Integer,String&gt; 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 = &quot;The string 1s is use 2 test the map number3.&quot;;
HashMap&lt;Integer,String&gt; map = new HashMap&lt;&gt;();
map.put(1,&quot;one&quot;);
map.put(2,&quot;two&quot;);
map.put(3,&quot;three&quot;);
for (Map.Entry&lt;Integer,String&gt; 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.

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:

确定