您的位置:首页 > 其它

已知:yi er san si wu liu qi ba jiu 分别对应123456789, 对一段只含有这几种字符串的字符串进行转换

2014-07-09 13:00 447 查看
功能描述:已知:yi er san si wu liu qi ba jiu 分别对应123456789, 对一段只含有这几种字符串的字符串进行转换,如:
 输入:yiersansan 输出:1233

分析:使用String 的 replaceAll()方法对字符串用数字进行替换。import java.util.Scanner;

public class Test26
{
public static final String[] REGEX = {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
public static void main(String[] args)
{
System.out.println("Input the String:");
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
System.out.println(new Test26().replace(str));
}

//替换
public String replace(String str)
{
for (int i = 0; i < str.length(); i++)
{
str = str.replaceAll(REGEX[i], i + "");
}

return str;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐