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

功能描述:已知: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;
	}
}

你可能感兴趣的:(机试题)