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

242 阅读1分钟

题目:

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

给定3个字符串str from to已知from字符串无重复字符,把str中所有from的子串全都替换成to字符串,连续出现from只需要换成一个to就可。

例如:

str="123adc" from = "adc" to ="4567" 返回1234567

str="123" from = "adc" to ="4567" 返回123

str="123adcabc" from = "adc" to ="X" 返回123X

 

实现代码:

 

package com.chenyu.string.cn;

public class RaplaceString {
	
	public static void main(String[] args) {
		
		String target = "000abcabc123abc456abc789abcabc";
		String from = "abc";
		String to = "def";
		String result = replace(target, from ,to);
		System.out.print("result:" + result);
		
	}
	
	
	public static String replace(String target, String from, String to) {
		
		if (target == null || "".equ