1. 程式人生 > >替換字串中的%s,並追加剩餘部分

替換字串中的%s,並追加剩餘部分

/**  * 替換字串中的%s,剩餘的在後面追加  * A%sC%sE          ["B","D","F","G"]  * ------->  * ABCDEFG  */

/**
 * 替換字串中的%s,剩餘的在後面追加
 * A%sC%sE          ["B","D","F","G"]
 * ------->
 * ABCDEFG
 */
public class Main {
	
	public static String insertNum(String str,String[] array) {
		int num = 0;
		//先進行替換
		while(str.contains("%s")) {
			str = str.replaceFirst("%s", array[num++]);
		}
		//再進行追加
		for(int i=num;i<array.length;i++) {
			str = str+array[i];
		}
		return str;
	}
	public static void main(String[] args) {
		//一個字串,一個String類陣列
		String string = "A%sC%sE%s";
		String[] array = {"B","D","F","G","H","I","J"};
		System.out.println(insertNum(string, array));
	}
}