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?

13
  • 1
    @SotiriosDelimanolis: I have started my career with Java 1.2, so there are few. Commented Nov 13, 2013 at 20:59
  • 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?" Commented Nov 13, 2013 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 Commented Nov 13, 2013 at 21:01
  • 3
    @StormeHawke this is likely faster than the regular expression replaceAll actually. You should post this as an answer. Commented Nov 13, 2013 at 21:02
  • 1
    @Cruncher I think somebody grabbed my idea already. No worries, question's been answered Commented Nov 13, 2013 at 21:03

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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: 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.
by the docs, it has been around since the start. (At least there's no "Since:" section)
@quazzieclodo: Many thanks for looking over the docs. I have updated the answer accordingly.
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.

3 Comments

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).
lastIndexOf(String) does not handle regular expressions, I believe. The Pattern class was not even in java until 1.4
@quazzieclodo: Though you are completely correct, I believe I beat you to the punch by about 21 seconds :-)
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.

3 Comments

Are sure modern libs are 1.3-compatible? :-)
My guess is that this library may use replaceAll under the hood.
Not to mention at the top of the site it says it has been retired for over two years.
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());
}

1 Comment

I believe this to be the best solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.