简体   繁体   English

Java 1.3 String.replaceAll(),替换

[英]Java 1.3 String.replaceAll() , replacement

I'm working with some really old Java. 1.3 我正在使用一些非常古老的Java. 1.3 Java. 1.3 to be exact. Java. 1.3确切地说。

I'm attempting to sanitize some String input by removing non alphabet characters (punctuation and numbers, etc) 我试图通过删除非字母字符(标点符号和数字等)来清理一些String输入

Normally I'd do something like: 通常我会做类似的事情:

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

However, .replaceAll() was introduced in Java 1.4 ! 但是, .replaceAll()是在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) 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 ? 我们是如何在Java 1.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. 抱歉没有JDK1.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. 当你想要擦除的不只是一个字符时,我建议你调整这个代码以允许一组needles或(丑陋的方法)循环不允许的字符并重复调用该函数。

You can use jakarta by the Apache Software Foundation. 您可以使用Apache Software Foundation的jakarta

Jakarta Regexp is a 100% Pure Java Regular Expression package Jakarta Regexp是一个100%纯Java正则表达式包

It's not maintained but the last version is not so old (2011). 它没有维护,但最后一个版本不是那么老(2011)。

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

For the replaceAll you can use subst with the REPLACE_ALL flag. 对于replaceAll,您可以使用带有REPLACE_ALL标志的subst

PS: The link is dead, here a mirror to download the lib . PS:链接已经死了, 这里有一个下载lib的镜像

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. 如果有帮助,请参阅http://docs.oracle.com/javase/1.3/docs/api/java/lang/Character.html

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());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM