PlotSquared/src/main/java/com/intellectualcrafters/plot/util/StringMan.java
2015-07-22 03:08:51 +10:00

49 lines
1.7 KiB
Java

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);
int size = string.length();
for (Entry<String, String> entry : replacements.entrySet()) {
if (size == 0) {
break;
}
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);
size -= end - start;
start = sb.indexOf(key, nextSearchStart);
}
}
return sb.toString();
}
public static String replaceAll(String string, Object... pairs) {
StringBuilder sb = new StringBuilder(string);
for (int i = 0; i < pairs.length; i+=2) {
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);
}
}
return sb.toString();
}
public static boolean isEqual(String a, String b ) {
return (a == b || (a.length() == b.length() && a.hashCode() == b.hashCode() && a.equals(b)));
}
}