3

I'm working with some really old Java. 1.3 to be exact.

I'm attempting to sanitize some String input by removing non alphabet characters (punctuation and numbers, etc)

Normally I'd do something like:

String.replaceAll("[^A-Za-z]", "");

However, .replaceAll() was introduced in Java 1.4! So it won't compile! http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replaceAll(java.lang.String, java.lang.String)

How did we accomplish this prior to Java 1.4?

SnakeDoc
  • 13,611
  • 17
  • 65
  • 97
  • @SotiriosDelimanolis haha! yes, not fun to maintain this particular codebase... *sigh* - i think 1.3 was circa 2000? – SnakeDoc Nov 13 '13 at 20:56
  • You may look at replaceAll() in latest source and try to replicate that in 1.3 – kosa Nov 13 '13 at 20:57
  • @Nambari hmm, a possibility. Although, if it's like the other String methods, it uses a lot of internal vars from within the String object. -- even if I could, it feels "dirty" to do it that way. Surely Java Devs in the early 2000's had a need to replace chars in Strings easily? – SnakeDoc Nov 13 '13 at 20:58
  • 1
    @SotiriosDelimanolis: I have started my career with Java 1.2, so there are few. – kosa Nov 13 '13 at 20:59
  • @Nambari, if so, then they must be too old ;) – Sage Nov 13 '13 at 21:00
  • Refer to String API docs http://docs.oracle.com/javase/1.3/docs/api/java/lang/String.html. pay special attention to the indexOf() and substring() methods. – user1769790 Nov 13 '13 at 21:01
  • 2
    @SotiriosDelimanolis Java 1.3 exists. I'm alive. So yes ;). I think a better question would be: "Was anyone on stackoverflow even alive before Java 1.4 existed?" – Cruncher Nov 13 '13 at 21:01
  • 5
    Stripped of my trusty `replaceAll()` method, were it me, I would iterate over a `toCharArray()` version of the `String` and build a new string, skipping any characters that fall outside of the alphabet range – StormeHawke Nov 13 '13 at 21:01
  • 3
    @StormeHawke this is likely faster than the regular expression replaceAll actually. You should post this as an answer. – Cruncher Nov 13 '13 at 21:02
  • Noting that you can freely reference chars as ints and test whether they're in the range of ascii codes represented by [A-Za-z]. This works for ASCII... I'm probably presuming a lot by that – StormeHawke Nov 13 '13 at 21:02
  • Or start from http://docs.oracle.com/javase/1.3/docs/api/java/lang/String.html#replace(char, char) – Eugen Constantin Dinca Nov 13 '13 at 21:02
  • 1
    @Cruncher I think somebody grabbed my idea already. No worries, question's been answered – StormeHawke Nov 13 '13 at 21:03
  • Java 1.1 for me. But Java 1.3 is so old, it didn't even get an End Of Life date, Java 1.4 was EOL 5 years ago. – Peter Lawrey Nov 13 '13 at 21:39

4 Answers4

4

Well probably you can write a simple loop like this:

char[] origArr = str.toCharArray();
char[] destArr = new char[origArr.length];
int j = 0;
for (int i=0; i < origArr.length; i++) {
    char c = origArr[i];
    if ((c >= 65 && c <= 90) || (c >= 97 && c <= 122))
       destArr[j++] = c;
}

String dest = new String(destArr, 0, j);

Sorry don't have JDK1.3 to test it out.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    This is what I would do. However, probably change the final constructor to `String dest = new String(destArr, 0, j)` to make sure `dest` isn't doing anything with those extra Array elements. – quazzieclodo Nov 13 '13 at 21:09
  • @quazzieclodo: Yes I thought about it but wasn't sure if that constructor was available in JDK 1.3 but yes it is available then **`String dest = new String(destArr, 0, j);`** should be used in the end. – anubhava Nov 13 '13 at 21:12
  • by the [docs](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#String%28char[],%20int,%20int%29), it has been around since the start. (At least there's no "Since:" section) – quazzieclodo Nov 13 '13 at 21:17
  • @quazzieclodo: Many thanks for looking over the docs. I have updated the answer accordingly. – anubhava Nov 13 '13 at 21:20
3

See if this works:

public static String replaceAll(
    String haystack,              // String to search in
    String needle,                // Substring to find
    String replacement) {         // Substring to replace with

    int i = haystack.lastIndexOf( needle );
    if ( i != -1 ) {
        StringBuffer buffer = new StringBuffer( haystack );
        buffer.replace( i, i+needle.length(), replacement );
        while( (i=haystack.lastIndexOf(needle, i-1)) != -1 ) {
            buffer.replace( i, i+needle.length(), replacement );
        }
        haystack = buffer.toString();
    }

    return haystack;
}

EDIT: This won't support regular expressions. As you're looking to erase more than just a single character, I would suggest you either tweak this code to allow an array of needles or (the ugly method) loop through the disallowed characters and repeatedly call the function.

Luke
  • 409
  • 3
  • 12
  • Timer is still counting - but this compiled! I'm going to throw some data through it and see if the output ends up as expected (it should from what i see). – SnakeDoc Nov 13 '13 at 21:07
  • 1
    `lastIndexOf(String)` does not handle regular expressions, I believe. The Pattern class was not even in java until 1.4 – quazzieclodo Nov 13 '13 at 21:13
  • @quazzieclodo: Though you are completely correct, I believe I beat you to the punch by about 21 seconds :-) – Luke Nov 13 '13 at 21:14
1

You can use jakarta by the Apache Software Foundation.

Jakarta Regexp is a 100% Pure Java Regular Expression package

It's not maintained but the last version is not so old (2011).

The documentation: http://jakarta.apache.org/regexp/

For the replaceAll you can use subst with the REPLACE_ALL flag.

PS: The link is dead, here a mirror to download the lib.

Fabien Sa
  • 9,135
  • 4
  • 37
  • 44
1

Assuming you need only alphabets and anything else to be replaced with blank. Character also got isDigit method. please refer to http://docs.oracle.com/javase/1.3/docs/api/java/lang/Character.html if it helps.

public static void main (String[] args)
{


String yourstring = "2323ABF!@CD24";
char[] check = yourstring.toCharArray();
StringBuffer str = new StringBuffer();
for(int i=0; i < check.length; i++){
    if(!Character.isLetter(check[i])){
       str.append("");
    }
    else{
        str.append(check[i]);
    }
}
System.out.println(str.toString());
}
user1769790
  • 1,183
  • 3
  • 11
  • 23