【问题标题】:Converting String symbol into char将字符串符号转换为字符
【发布时间】:2014-09-11 16:47:30
【问题描述】:

我正在实施单字母替换算法。键和值存储在HashMap中,并使用StringBuffer类类型的变量作为输入字符串的参数:

public class Mono {
    private StringBuffer source;
    private Map<String, String> alphabet;

public Mono(String source) {
    this.source = new StringBuffer(source);
    alphabet = new HashMap<>();
    alphabet.put("a", "f");
    alphabet.put( //... etc.
}

public StringBuffer startEncrypt() {
        for (int i = 0; i < source.length(); i++) {
            for (Map.Entry entry : alphabet.entrySet()) {
                if (entry.getKey().toString().equals(source.charAt(i))) {
                    source.setCharAt(i, entry.getValue().toString());
                }
            }
        }

        return source;
    }
}

我在这里将字符串转换为字符时遇到问题:source.setCharAt(i, entry.getValue().toString());

那么,我的问题是如何做到这一点?还是有其他更好的替换字符的方法?

谢谢。

【问题讨论】:

    标签: java string stringbuffer


    【解决方案1】:

    您可以使用charAt(0) 来获取字符串的第一个字符...就像您已经为您的密钥所做的那样。请注意,如果您使用参数化的 Map.Entry 而不是原始类型,则也不需要 toString 调用:

    for (Map.Entry<String, String> entry : alphabet.entrySet()) {
        // TODO: Should this be source.charAt(i)?
        if (entry.getKey().equals(source.charAt(0))) {
            source.setCharAt(i, entry.getValue().charAt(0));
        }
    }
    

    也就是说,如果你所有的字符串真的只是一个字符,你为什么不使用Map&lt;Character, Character&gt;?此外,遍历地图中的所有项效率低下,几乎可以肯定不是您想要做的。我怀疑你实际上想要这样的东西:

    private final Map<Character, Character> mappings;
    
    public String substitute(String input) {
        char[] chars = input.toCharArray();
        for (int i = 0; < chars.length; i++) {
            Character sub = mapping.get(chars[i]);
            if (sub != null) {
                chars[i] = sub;
            }
        }
        return new String(chars);
    }
    

    【讨论】:

    • Should this be source.charAt(i)? 是的,你是对的 - 已修复。我还没有完全熟悉 Collections Framework,非常感谢您的帮助!
    【解决方案2】:

    尝试:

    entry.getValue().toString().chartAt(0)
    

    【讨论】:

      【解决方案3】:

      不确定这是否符合您的需求,但我想:

      public class Mono {
          private StringBuffer source;
          private Map<Character, Character> alphabet;
      
          public Mono(String source) {
              this.source = new StringBuffer(source);
              alphabet = new HashMap<>();
              alphabet.put('a', 'f');
              alphabet.put( //... etc.
          }
      
          public StringBuffer startEncrypt() {
              for (int i = 0; i < source.length(); i++) {
                  for (Character key : alphabet.keySet()) {
                      if (source.charAt(0) == key) {
                          source.setCharAt(i, alphabet.get(key));
                      }
                  }
              }
      
              return source;
          }
      }
      

      它更具可读性,您不需要任何类型转换。越简单越好:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-04
        • 1970-01-01
        相关资源
        最近更新 更多