Java 中如何替换汉字呢?
- /***
- * 把中文替换为指定字符<br>
- * 注意:一次只匹配一个中文字符
- * @param source
- * @param replacement
- * @return
- */
- public static String replaceChinese(String source, String replacement){
- if(ValueWidget.isNullOrEmpty(source)){
- return null;
- }
- if(replacement==null){
- replacement=SystemHWUtil.EMPTY;
- }
- String reg = "[\u4e00-\u9fa5]";
- Pattern pat = Pattern.compile(reg);
- Matcher mat=pat.matcher(source);
- String repickStr = mat.replaceAll(replacement);
- return repickStr;
- }
测试:
- public static void main(String[] args) {
- String str = "123你好aaa";
- String repickStr=RegexUtil.replaceChinese(str, "_");
- System.out.println("去中文后:"+repickStr);
- }
时间: 2024-12-05 09:00:33