-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringUtil.java
More file actions
51 lines (43 loc) · 1.43 KB
/
Copy pathStringUtil.java
File metadata and controls
51 lines (43 loc) · 1.43 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
class StringUtil {
/**
* Replaces all occurences of oldSubString in string with the newSubString.
* If ignoreCase replacements is asked, the search for oldSubString does
* a case insensitive search for the oldSubString.
*
* @param text the text for which the replacement has to be performed
* @param oldSubString the text which has to be replaced
* @param newSubString the text which has to replace the oldSubString
* occurrences
* @param ignoreCase <code>true</code> if the replacement should be case
* insensitive
* @return the text with all occurrences of oldSubString replaced with
* newSubString
*/
public static String replace(
String text,
String oldSubString,
String newSubString,
boolean ignoreCase) {
if (!ignoreCase){
return replace(text, oldSubString, newSubString,-1);
}else{
String searchString = text;
String searchPattern = oldSubString;
if (ignoreCase) {
searchString = searchString.toLowerCase();
searchPattern = searchPattern.toLowerCase();
}
int index = searchString.indexOf(searchPattern);
while (index != -1) {
StringBuffer buf = new StringBuffer(text);
buf.replace(index, index + searchPattern.length(), newSubString);
text = buf.toString();
searchString = text;
if (ignoreCase)
searchString = searchString.toLowerCase();
index = searchString.indexOf(searchPattern, index + newSubString.length());
}
return text;
}
}
}