格式化字符串-提取中文

Posted Vip灬cnblog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了格式化字符串-提取中文相关的知识,希望对你有一定的参考价值。

 1、原来的String

 {\'info\':[{\'menuId\':\'193\',\'menuName\':\'家装建材\'},{\'menuId\':\'194\',\'menuName\':\'家纺\'},{\'menuId\':\'176\',\'menuName\':\'手机通讯\'},{\'menuId\':\'178\',\'menuName\':\'手机配件\'}]}

 要求: 家装建材,家纺,手机通讯,手机配件

 

 /**
   *  格式化字符串
   * @param str
   * @return
   */
    public String SubString(String str){
        String substrZyhy="";
            Matcher matcher = Pattern.compile("([\\u4e00-\\u9fa5]+)").matcher(str);
            while (matcher.find() ) {
                substrZyhy+= matcher.group(0)+",";
           }
        return substrZyhy;
        
    }

     

//测试代码
public static void main(String[] args) {
                String  str=(" {\'info\':[{\'menuId\':\'193\',\'menuName\':\'家装建材\'},{\'menuId\':\'194\',\'menuName\':\'家纺\'},{\'menuId\':\'176\',\'menuName\':\'手机通讯\'},{\'menuId\':\'178\',\'menuName\':\'手机配件\'}]}");
                String substrZyhy="";
                Matcher matcher = Pattern.compile("([\\u4e00-\\u9fa5]+)").matcher(str);
                while (matcher.find() ) {
                        substrZyhy+= matcher.group(0)+",";
                }
                    System.out.println(substrZyhy);
    }
    

2、结果

 

以上是关于格式化字符串-提取中文的主要内容,如果未能解决你的问题,请参考以下文章