用Java替换外语为英语字符的方法? [英] Method to substitute foreign for English characters in Java?

查看:112
本文介绍了用Java替换外语为英语字符的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP中,我会使用:

In PHP I would use this:

$text = "Je prends une thé chaud, s'il vous plaît";
$search = array('é','î','è'); // etc.
$replace = array('e','i','e'); // etc.
$text = str_replace($search, $replace, $text); 

但是Java String方法replace似乎不接受数组作为输入。有没有办法做到这一点(不必求助于一个for循环通过数组)?

But the Java String method "replace" doesn't seem to accept arrays as input. Is there a way to do this (without having to resort to a for loop to go through the array)?

请说如果有一个更优雅的方法比方法我'

Please say if there's a more elegant way than the method I'm attempting.

推荐答案

一个非常好的方法是使用 replaceEach()方法从Apache Commons Lang 2.4中的 StringUtils

A really nice way to do it is using the replaceEach() method from the StringUtils class in Apache Commons Lang 2.4.

String text = "Je prends une thé chaud, s'il vous plaît";
String[] search = new String[] {"é", "î", "è"};
String[] replace = new String[] {"e", "i", "e"};
String newText = StringUtils.replaceEach(text, 
                search, 
                replace);

结果

Je prends une the chaud, s'il vous plait

这篇关于用Java替换外语为英语字符的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆