gpt4 book ai didi

Java - 检查 STRING 是否仅包含某些字符的最佳方法是什么?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:03:36 25 4
gpt4 key购买 nike

我有这个问题:我有一个 String,但我需要确保它包含字母 A-Z 和数字 0-9。这是我当前的代码:

boolean valid = true;
for (char c : string.toCharArray()) {
int type = Character.getType(c);
if (type == 2 || type == 1 || type == 9) {
// the character is either a letter or a digit
} else {
valid = false;
break;
}
}

但是最好和最有效的实现方式是什么?

最佳答案

因为还没有人担心“最快”,这里是我的贡献:

boolean valid = true;

char[] a = s.toCharArray();

for (char c: a)
{
valid = ((c >= 'a') && (c <= 'z')) ||
((c >= 'A') && (c <= 'Z')) ||
((c >= '0') && (c <= '9'));

if (!valid)
{
break;
}
}

return valid;

完整测试代码如下:

public static void main(String[] args)
{
String[] testStrings = {"abcdefghijklmnopqrstuvwxyz0123456789", "", "00000", "abcdefghijklmnopqrstuvwxyz0123456789&", "1", "q", "test123", "(#*$))&v", "ABC123", "hello", "supercalifragilisticexpialidocious"};

long startNanos = System.nanoTime();

for (String testString: testStrings)
{
isAlphaNumericOriginal(testString);
}

System.out.println("Time for isAlphaNumericOriginal: " + (System.nanoTime() - startNanos) + " ns");

startNanos = System.nanoTime();

for (String testString: testStrings)
{
isAlphaNumericFast(testString);
}

System.out.println("Time for isAlphaNumericFast: " + (System.nanoTime() - startNanos) + " ns");

startNanos = System.nanoTime();

for (String testString: testStrings)
{
isAlphaNumericRegEx(testString);
}

System.out.println("Time for isAlphaNumericRegEx: " + (System.nanoTime() - startNanos) + " ns");

startNanos = System.nanoTime();

for (String testString: testStrings)
{
isAlphaNumericIsLetterOrDigit(testString);
}

System.out.println("Time for isAlphaNumericIsLetterOrDigit: " + (System.nanoTime() - startNanos) + " ns");
}

private static boolean isAlphaNumericOriginal(String s)
{
boolean valid = true;
for (char c : s.toCharArray())
{
int type = Character.getType(c);
if (type == 2 || type == 1 || type == 9)
{
// the character is either a letter or a digit
}
else
{
valid = false;
break;
}
}

return valid;
}

private static boolean isAlphaNumericFast(String s)
{
boolean valid = true;

char[] a = s.toCharArray();

for (char c: a)
{
valid = ((c >= 'a') && (c <= 'z')) ||
((c >= 'A') && (c <= 'Z')) ||
((c >= '0') && (c <= '9'));

if (!valid)
{
break;
}
}

return valid;
}

private static boolean isAlphaNumericRegEx(String s)
{
return Pattern.matches("[\\dA-Za-z]+", s);
}

private static boolean isAlphaNumericIsLetterOrDigit(String s)
{
boolean valid = true;
for (char c : s.toCharArray()) {
if(!Character.isLetterOrDigit(c))
{
valid = false;
break;
}
}
return valid;
}

为我生成这个输出:

Time for isAlphaNumericOriginal: 164960 ns
Time for isAlphaNumericFast: 18472 ns
Time for isAlphaNumericRegEx: 1978230 ns
Time for isAlphaNumericIsLetterOrDigit: 110315 ns

关于Java - 检查 STRING 是否仅包含某些字符的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26555346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com