替换字符串中连续出现的指定字符串

【题目】给定三个字符串str、from和to,已知from字符串中无重复字符,把str中所有from的子串全部替换成to字符串,对连续出现from的部分要求只替换成一个to字符串,返回最终的结果字符串。

【举例】str = “123abc”,from = “abc”,to = “4567”,返回“1234567”。

​ str = “123”,from = “abc”,to = “456”,返回“123”。

public class StrConvertFrom {
	public static String strConvertFrom(String str, String from, String to) {
		if (str == null || from == null || str.equals("") || from.equals("")) {
			return str;
		}
		char[] chas = str.toCharArray();
		char[] chaf = from.toCharArray();
		int match = 0;
		for (int i = 0; i < chas.length; i++) {
			if (chas[i] == chaf[match++]) {
				if (match == chaf.length) {
					clear(chas, i, chaf.length);
					match = 0;
				}
			} else {
				if (chas[i] == chaf[0]) {
					i--;
				}
				match = 0;
			}
		}
		String res = "";
		String cur = "";
		for (int i = 0; i < chas.length; i++) {
			if (chas[i] != 0) {
				cur = cur + String.valueOf(chas[i]);
			}
			if (chas[i] == 0 && (i == 0 || chas[i - 1] != 0)) {
				res = res + cur + to;
				cur = "";
			}
		}
		if (!cur.equals("")) {
			res = res + cur;
		}
		return res;
	}

	public static void clear(char[] chas, int end, int len) {
		while (len-- != 0) {
			chas[end--] = 0;
		}
	}
}

猜你喜欢

转载自blog.csdn.net/gkq_tt/article/details/86603454
今日推荐