简体   繁体   English

如何检查字符串不是字母或数字的字符串

[英]How can I check is strings with characters that are not letters or number

I am trying to trim any characters that are before aZ 0-9 我正在尝试修剪aZ 0-9之前的所有字符

but this doesn't work 但这不起作用

I need >&%Hell$o to become Hell$o 我需要>&%Hell$o才能成为Hell$o

private String removeStartingCharacters(String linkName) {
    if(linkName.startsWith("^[a-Z0-9]")){
        try {
            linkName = linkName.substring(1);               
        } catch (Exception e) {
            e.printStackTrace();
            return linkName;
        }
        return removeStartingCharacters(linkName);
    }else 
        return linkName;
}

you can just use Character.isLetterOrDigit(char) : 您可以只使用Character.isLetterOrDigit(char)

private String removeStartingCharacters(String linkName) {
    if (!Character.isLetterOrDigit(linkName.cahrAt(0)){
        try {
            linkName = linkName.substring(1);               
        } catch (Exception e) {
            e.printStackTrace();
            return linkName;
        }
        return removeStartingCharacters(linkName);
    } else 
        return linkName;
}

I think this is what you're after: 我认为这是您追求的目标:

public class Test {
    public static void main(String[] args) {
        System.out.println(trimStart("&%Hell$o"));
        // [ and ] are between A-Z and a-z...
        System.out.println(trimStart("[]Hell$o"));
        System.out.println(trimStart("Hell$o"));
    }

    private static String trimStart(String input) {
        // The initial ^ matches only at the start of the string
        // The [^A-Za-z0-9] matches all characters *except* A-Z, a-z, 0-9
        // The + matches at least one character. (The output is the same
        // as using * in this case, as if there's nothing to replace it would
        // just replace the empty string with itself...)
        return input.replaceAll("^[^A-Za-z0-9]+", "");
    }
}

(The output shows Hell$o in all cases.) (输出在所有情况下都显示Hell$o 。)

A single call to replaceAll is significantly simpler than what you're doing at the moment. 一次调用replaceAll比您当前正在执行的操作要简单得多。

EDIT: replaceFirst will work too, but you do still need the ^ at the start to make sure it only replaces characters from the start of the string. 编辑: replaceFirst也可以工作,但仍然需要^在开始,以确保它只能从字符串的开头替换字符。 The "first" in replaceFirst just means the first occurrence of the pattern, not the first characters within the input string. replaceFirst的“ first”仅表示模式的第一个匹配项,而不是输入字符串中的第一个字符。 Use whichever method you find more readable. 使用任何您认为更易读的方法。

Note that this only allows az, AZ and 0-9 as your starting characters: 请注意,这允许使用az,AZ和0-9作为起始字符:

  • It doesn't allow the characters between Z and a (eg [ and ] ) 不允许Z和a之间的字符(例如[]
  • It doesn't allow non-ASCII letters or digits 不允许使用非ASCII字母或数字

You'll need to adjust the regex if those rules don't match your actual requirements 如果这些规则与您的实际要求不符,则需要调整正则表达式

A Pattern -based solution would be as such: 基于Pattern的解决方案将是这样的:

public static String removeStartingNonAlnums(String input) {
    // null check
    if (input == null) {
        return null;
    }
    // empty check
    else if (input.isEmpty()) {
        return "";
    }
    // pattern check
    else {
                              // | start of input
                              // | | negation of alpha-numeric category
                              // | |        | zero/more greedy quantifier
                              // | |        |   | replaces with empty
        return input.replaceAll("^\\P{Alnum}*", "");
    }

}

... wherein ">&%Hell$o" becomes Hell$o . ...,其中">&%Hell$o"变为Hell$o

str.replaceFirst("^[^a-zA-Z0-9]+", "");

您可以简单地使用replaceFirst替换[a-zA-Z_0-9]不包含的字符,重新添加下划线以删除该字符

input = input.replaceFirst("^[\\W_]+", "");

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

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