2015-07-05 17:44:10 +02:00
|
|
|
package com.intellectualcrafters.plot.util;
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
|
|
|
|
public class StringMan {
|
|
|
|
public static String replaceFromMap(String string, Map<String, String> replacements) {
|
|
|
|
StringBuilder sb = new StringBuilder(string);
|
2015-07-17 12:48:13 +02:00
|
|
|
int size = string.length();
|
2015-07-05 17:44:10 +02:00
|
|
|
for (Entry<String, String> entry : replacements.entrySet()) {
|
2015-07-17 12:48:13 +02:00
|
|
|
if (size == 0) {
|
|
|
|
break;
|
|
|
|
}
|
2015-07-05 17:44:10 +02:00
|
|
|
String key = entry.getKey();
|
|
|
|
String value = entry.getValue();
|
|
|
|
int start = sb.indexOf(key, 0);
|
|
|
|
while (start > -1) {
|
|
|
|
int end = start + key.length();
|
|
|
|
int nextSearchStart = start + value.length();
|
|
|
|
sb.replace(start, end, value);
|
2015-07-17 12:48:13 +02:00
|
|
|
size -= end - start;
|
2015-07-05 17:44:10 +02:00
|
|
|
start = sb.indexOf(key, nextSearchStart);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sb.toString();
|
|
|
|
}
|
2015-07-16 16:44:27 +02:00
|
|
|
|
|
|
|
public static String replaceAll(String string, Object... pairs) {
|
2015-07-17 12:48:13 +02:00
|
|
|
StringBuilder sb = new StringBuilder(string);
|
2015-07-16 16:44:27 +02:00
|
|
|
for (int i = 0; i < pairs.length; i+=2) {
|
2015-07-17 12:48:13 +02:00
|
|
|
String key = pairs[i] + "";
|
|
|
|
String value = pairs[i + 1] + "";
|
|
|
|
int start = sb.indexOf(key, 0);
|
|
|
|
while (start > -1) {
|
|
|
|
int end = start + key.length();
|
|
|
|
int nextSearchStart = start + value.length();
|
|
|
|
sb.replace(start, end, value);
|
|
|
|
start = sb.indexOf(key, nextSearchStart);
|
|
|
|
}
|
2015-07-16 16:44:27 +02:00
|
|
|
}
|
2015-07-17 12:48:13 +02:00
|
|
|
return sb.toString();
|
2015-07-16 16:44:27 +02:00
|
|
|
}
|
2015-07-21 19:08:51 +02:00
|
|
|
|
|
|
|
public static boolean isEqual(String a, String b ) {
|
|
|
|
return (a == b || (a.length() == b.length() && a.hashCode() == b.hashCode() && a.equals(b)));
|
|
|
|
}
|
|
|
|
|
2015-07-05 17:44:10 +02:00
|
|
|
}
|