So I am working on a project that for certain reasons is limited to java squawk 1.4. Because of this, the String class does not contain the four methods in the title. I need those methods in my program, and have concluded that I have to make a Util class that performs the functions of those methods by itself.
First off, does this exist somewhere? Obviously my first reaction was to look into just copying the source from the String class, but the dependencies of those methods go far too deep for me to use.
Secondly, I am having trouble replicating the behaviour of split(String regex) and replace(CharSequence target, CharSequence replacement). contains(String) and isEmpty() are easy obviously, but I have run into trouble coding the others.
Right now, I have split working (Although it works in a different way than in jdk 7, and I do not want to get bugs).
public static String[] split(String string, char split) {
String[] s = new String[0];
int count = 0;
for (int x = 0; x < string.length(); x++) {
if (string.charAt(x) == split) {
String[] tmp = s;
s = new String[++count];
System.arraycopy(tmp, 0, s, 0, tmp.length);
s[count - 1] = string.substring(x).substring(1);
if (contains(s[count - 1], split + "")) {
s[count - 1] = s[count - 1].substring(0, s[count - 1].indexOf(split));
}
}
}
return s.length == 0 ? new String[]{string} : s;
}
Replace is much harder to do, and I have been trying for hours now. This seems to be a question google / archives have never even ventured.
Made the methods…