String  str ="jhuywy48r在74yhf47tf6中7dgc782有3y...";   后面还有几百个汉字  汉字的个数不确定我想把里面所有的 汉字  换成对应的 拼音     
我已经有 汉字 和  拼音  的 替换数组了那怎么有效率的  用这个数组  去把里面的汉字 替换掉了?

解决方案 »

  1.   

    1、把汉字和拼音做成一个通过对汉字 做一个哈希表 
    2、for循环遍历 ,然后替换 俺只会笨办法 ,静等高效方法...
      

  2.   

    public static void main(String[] args) throws Exception {
    String str ="jhuywy48r在74yhf47tf6中7dgc782有3y..."; 

    StringBuffer buf = new StringBuffer();

    Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
    Matcher m = p.matcher(str);
    while(m.find()){
    String chinese = m.group();//匹配出的中文
    String pinyin = "pinyin";//在你的中文与拼音对应中找到对应拼音。
    m.appendReplacement(buf, pinyin);
    }
    m.appendTail(buf);
    System.out.println(buf.toString());
    }
      

  3.   


    map查找的时间复杂度是O(1),难道还有更高效的?
      

  4.   


    正堂hashmap替换应该是最快的了应该是
      

  5.   

    http://www.chineselinuxuniversity.net/articles/25481.shtml这个貌似不错,跟1楼的想法是一样的!!!然后结合4楼的正则判断应该没问题了