-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepalceString.java
More file actions
81 lines (69 loc) · 1.78 KB
/
Copy pathRepalceString.java
File metadata and controls
81 lines (69 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package com.StrQuestions;
import java.util.LinkedList;
/**
* hzy:jdk1.8
*/
/**
* @author 残月
* @descripition 提供三个字符串str、from、to将str中包含from的子串替换为to, 连续的from子串只替换一个to
* @ieda 使用队列记录from在str中出现的开始下标和结束下标;
*/
public class RepalceString {
/**
*
*/
public RepalceString() {
// TODO Auto-generated constructor stub
}
public static String replace(String str, String from, String to) {
if (str == null || from == null || str.equals("") || from.equals(""))
return str;
char[] chars = str.toCharArray();
char[] charf = from.toCharArray();
int len = charf.length;
int index = 0;
LinkedList<Integer> ls = new LinkedList<Integer>();
for (int i = 0; i < chars.length; i++) {
if (chars[i] == charf[index++]) {
if (index == len) {
ls.add(i - len + 1);
ls.add(i);
index = 0;
}
} else {
index = 0;
}
}
String cur = "";
String res = "";
int num = 0;
if (ls.peekFirst() == 0) {
res=to;
ls.pollFirst();
num=ls.pollFirst()+1;
}
return replaceNumnotequal0(num, ls, str, cur, res, to);
}
private static String replaceNumnotequal0(int num, LinkedList<Integer> ls, String str, String cur, String res,
String to) {
// TODO Auto-generated method stub
while (!ls.isEmpty()) {
if (num == ls.peekFirst()) {
ls.pollFirst();
num = ls.pollFirst() + 1;
} else {
cur = str.substring(num, ls.pollFirst());
num = ls.pollFirst() + 1;
res = res + cur + to;
}
}
if (num <= str.length() - 1) {
res = res + str.substring(num);
}
return res;
}
public static void main(String[] args) {
String str = "abc123abcabc567abc";
System.out.print(replace(str, "abc", "X"));
}
}