ChatGPT解决这个技术问题 Extra ChatGPT

Java String - 查看字符串是否仅包含数字而不包含字母

我在整个应用程序中加载了一个字符串,它从数字变为字母等。我有一个简单的 if 语句来查看它是否包含字母或数字,但是有些东西不能正常工作。这是一个片段。

String text = "abc"; 
String number; 

if (text.contains("[a-zA-Z]+") == false && text.length() > 2) {
    number = text; 
}

尽管 text 变量确实包含字母,但条件返回为 true。和 && 应该评估为两个条件都必须是 true 才能处理 number = text;

===============================

解决方案:

我能够通过使用对此问题的评论提供的以下代码来解决此问题。所有其他帖子也有效!

我使用的有效来自第一条评论。尽管提供的所有示例代码似乎也是有效的!

String text = "abc"; 
String number; 

if (Pattern.matches("[a-zA-Z]+", text) == false && text.length() > 2) {
    number = text; 
}
contains 不接受正则表达式作为输入。使用 matches("\\d{2,}") 或尝试使用 PatternMatcher
字符串可以有十进制值还是只有整数值?
为什么要检查 text.length() > 2?什么原因?
@RedHatcc Pattern.matches("[a-zA-Z]+", text) == false 可以简化为 !Pattern.matches("[a-zA-Z]+", text)
使用 java 流 API boolean isNumeric = someString.chars().allMatch(x -> Character.isDigit(x)); 表单 Max Malysh 发布。

u
unwichtich

如果您要将数字作为文本处理,请更改:

if (text.contains("[a-zA-Z]+") == false && text.length() > 2){

至:

if (text.matches("[0-9]+") && text.length() > 2) {

与其检查字符串是否不包含字母字符,不如检查以确保它仅包含数字。

如果您确实想使用数值,请使用 Integer.parseInt()Double.parseDouble(),正如其他人在下面解释的那样。

附带说明一下,将布尔值与 truefalse 进行比较通常被认为是不好的做法。只需使用 if (condition)if (!condition)


您可能想要添加锚点(例如 ^[0-9]+$),否则 abc123def 将被视为一个数字。
我不认为这是必需的。 matches() 当且仅当它从头到尾完全匹配时才返回 true。
"^-?\d+\.?\d*$" 将比较整个字符串,并且仅在它是有效数字(包括负数和小数)时才匹配。例如,它将匹配 1、10、1.0、-1、-1.0 等。它还将匹配“1”。但这通常可以被解析。
无需调用 && (text.length() > 2)。一切都可以在正则表达式模式中检查:if (text.matches("[0-9]{3,}")
不是整数的数字的逗号或点呢?
h
hannojg

您还可以使用 Apache Commons 中的 NumberUtil.isCreatable(String str)


我认为 NumberUtil.isCreatable(String str) 不适合用于原始问题的要求。例如,NumberUtil.isCreatable( "09" ) 返回 false,即使 "09" 仅包含数字
甚至 NumberUtils.isCreatable("068907") 都会返回 false
t
tokhi

我会这样做:

if(text.matches("^[0-9]*$") && text.length() > 2){
    //...
}

$ 将避免部分匹配,例如; 1B


我不需要 text.length() > 2 部分,所以我只是将 ^[0-9]*$ 替换为 ^[0-9]+$ 以确保我至少有一个数字。
text.matches("^[0-9]*$")text.matches("[0-9]*") 相同。
A
Aman Kumar Gupta

为了简单地检查它只包含 ALPHABETS 的字符串,请使用以下代码:

if (text.matches("[a-zA-Z]+"){
   // your operations
}

为了简单地检查它只包含 NUMBER 的字符串,请使用以下代码:

if (text.matches("[0-9]+"){
   // your operations
}

希望这对某人有帮助!


A
Anton R

性能方面的 parseInt 等比其他解决方案差得多,因为至少需要异常处理。

我已经运行了 jmh 测试,发现使用 charAt 迭代字符串并将字符与边界字符进行比较是测试字符串是否仅包含数字的最快方法。

JMH 测试

测试比较 Character.isDigitPattern.matcher().matchesLong.parseLong 与检查 char 值的性能。

这些方法可以为非 ascii 字符串和包含 +/- 符号的字符串产生不同的结果。

测试在吞吐量模式下运行(越大越好),有 5 次预热迭代和 5 次测试迭代。

结果

请注意,对于第一次测试加载,parseLong 几乎比 isDigit 慢 100 倍。

## Test load with 25% valid strings (75% strings contain non-digit symbols)

Benchmark       Mode  Cnt  Score   Error  Units
testIsDigit    thrpt    5  9.275 ± 2.348  ops/s
testPattern    thrpt    5  2.135 ± 0.697  ops/s
testParseLong  thrpt    5  0.166 ± 0.021  ops/s

## Test load with 50% valid strings (50% strings contain non-digit symbols)

Benchmark              Mode  Cnt  Score   Error  Units
testCharBetween       thrpt    5  16.773 ± 0.401  ops/s
testCharAtIsDigit     thrpt    5  8.917 ± 0.767  ops/s
testCharArrayIsDigit  thrpt    5  6.553 ± 0.425  ops/s
testPattern           thrpt    5  1.287 ± 0.057  ops/s
testIntStreamCodes    thrpt    5  0.966 ± 0.051  ops/s
testParseLong         thrpt    5  0.174 ± 0.013  ops/s
testParseInt          thrpt    5  0.078 ± 0.001  ops/s

测试套件

@State(Scope.Benchmark)
public class StringIsNumberBenchmark {
    private static final long CYCLES = 1_000_000L;
    private static final String[] STRINGS = {"12345678901","98765432177","58745896328","35741596328", "123456789a1", "1a345678901", "1234567890 "};
    private static final Pattern PATTERN = Pattern.compile("\\d+");

    @Benchmark
    public void testPattern() {
        for (int i = 0; i < CYCLES; i++) {
            for (String s : STRINGS) {
                boolean b = false;
                b = PATTERN.matcher(s).matches();
            }
        }
    }

    @Benchmark
    public void testParseLong() {
        for (int i = 0; i < CYCLES; i++) {
            for (String s : STRINGS) {
                boolean b = false;
                try {
                    Long.parseLong(s);
                    b = true;
                } catch (NumberFormatException e) {
                    // no-op
                }
            }
        }
    }

    @Benchmark
    public void testCharArrayIsDigit() {
        for (int i = 0; i < CYCLES; i++) {
            for (String s : STRINGS) {
                boolean b = false;
                for (char c : s.toCharArray()) {
                    b = Character.isDigit(c);
                    if (!b) {
                        break;
                    }
                }
            }
        }
    }

    @Benchmark
    public void testCharAtIsDigit() {
        for (int i = 0; i < CYCLES; i++) {
            for (String s : STRINGS) {
                boolean b = false;
                for (int j = 0; j < s.length(); j++) {
                    b = Character.isDigit(s.charAt(j));
                    if (!b) {
                        break;
                    }
                }
            }
        }
    }

    @Benchmark
    public void testIntStreamCodes() {
        for (int i = 0; i < CYCLES; i++) {
            for (String s : STRINGS) {
                boolean b = false;
                b = s.chars().allMatch(c -> c > 47 && c < 58);
            }
        }
    }

    @Benchmark
    public void testCharBetween() {
        for (int i = 0; i < CYCLES; i++) {
            for (String s : STRINGS) {
                boolean b = false;
                for (int j = 0; j < s.length(); j++) {
                    char charr = s.charAt(j);
                    b = '0' <= charr && charr <= '9';
                    if (!b) {
                        break;
                    }
                }
            }
        }
    }
}

更新于 2018 年 2 月 23 日

添加另外两种情况 - 一种使用 charAt 而不是创建额外的数组,另一种使用 IntStream 的 char 代码

如果为循环测试用例找到非数字,则添加立即中断

为循环测试用例的空字符串返回 false

更新于 2018 年 2 月 23 日

再添加一个测试用例(最快的!),在不使用流的情况下比较 char 值


如果您查看 toCharArray 的代码,它正在分配一个字符数组并复制字符(我认为这可能很昂贵)。如果您只使用索引和 charAt 迭代字符串,会更快吗?如果您可以将 Andy 的解决方案添加到您的测试中也会很有趣: boolean isNum = text.chars().allMatch(c -> c >= 48 && c <= 57)
A
Abdull

Apache Commons Lang 提供 org.apache.commons.lang.StringUtils.isNumeric(CharSequence cs),它将 String 作为参数并检查它是否由纯数字字符(包括来自非拉丁脚本的数字)组成。如果存在空格、减号、加号等字符以及逗号和点等小数分隔符,则该方法返回 false

该类的其他方法允许进一步的数字检查。


A
Andy

boolean isNum = text.chars().allMatch(c -> c >= 48 && c <= 57)


为了减少幻数,您可以进行如下比较:boolean isNum = text.chars().allMatch(c -> c >= '0' && c <= '9')
U
Unheilig

下面的正则表达式可用于检查字符串是否只有数字:

if (str.matches(".*[^0-9].*")) or if (str.matches(".*\\D.*"))

如果 String 包含非数字,上述两个条件都将返回 true。在 false 上,字符串只有数字。


Y
Yannick Huber

您可以使用 Regex.Match

if(text.matches("\\d*")&& text.length() > 2){
    System.out.println("number");
}

或者,您可以使用 Integer.parseInt(String) 或更好的 Long.parseLong(String) 之类的版本来获取更大的数字,例如:

private boolean onlyContainsNumbers(String text) {
    try {
        Long.parseLong(text);
        return true;
    } catch (NumberFormatException ex) {
        return false;
    }
} 

然后测试:

if (onlyContainsNumbers(text) && text.length() > 2) {
    // do Stuff
}

.matches("^\\d+$")
J
Jafar Karuthedath

使用 Java 8 流和 lambda 的解决方案

String data = "12345";
boolean isOnlyNumbers = data.chars().allMatch(Character::isDigit);

p
pseudoramble

在 Java 中有很多工具可以从 String 中获取数字(反之亦然)。您可能想跳过正则表达式部分,以免造成复杂性。

例如,您可以尝试查看 Double.parseDouble(String s) 为您返回什么。如果在字符串中找不到合适的值,它应该抛出 NumberFormatException。我建议使用这种技术,因为您实际上可以将 String 表示的值用作数字类型。


使用异常作为测试输入的原因可能不是一个好主意,异常会产生很大的开销。
@OfirLuzon 我同意例外不是处理即将出现的预期情况的好方法。但是,我认为如果没有更多的上下文,很难判断是否会影响性能。
U
Usman Javaid
import java.util.*;

class Class1 {
    public static void main(String[] argh) {
        boolean ans = CheckNumbers("123");
        if (ans == true) {
            System.out.println("String contains numbers only");
        } else {
            System.out.println("String contains other values as well");

        }
    }


    public static boolean CheckNumbers(String input) {
        for (int ctr = 0; ctr < input.length(); ctr++) {
            if ("1234567890".contains(Character.valueOf(input.charAt(ctr)).toString())) {
                continue;
            } else {
                return false;
            }
        }
        return true;
    }
}

S
Saurabh Gaddelpalliwar

这是我的代码,希望对你有帮助!

 public boolean isDigitOnly(String text){

    boolean isDigit = false;

    if (text.matches("[0-9]+") && text.length() > 2) {
        isDigit = true;
    }else {
        isDigit = false;
    }

    return isDigit;
}

F
F. Müller
StringUtils.isNumeric("1234")

这很好用。


T
Thiago

这是一个示例。根据需要仅查找字符串和进程格式中的数字。

text.replaceAll("\\d(?!$)", "$0 ");

有关更多信息,请查看 google Docs https://developer.android.com/reference/java/util/regex/Pattern 在哪里可以使用 模式


R
Ryan Stewart

这段代码已经写好了。如果您不介意(非常)次要的性能损失(这可能不比进行正则表达式匹配更差),请使用 Integer.parseInt()Double.parseDouble()。如果字符串只是数字(或 是一个 数字,视情况而定),它会立即告诉您。如果您需要处理更长的数字字符串,BigIntegerBigDecimal 都支持接受字符串的构造函数。如果您尝试传递一个非数字(整数或小数,当然取决于您选择的数字),其中任何一个都会抛出 NumberFormatException。或者,根据您的要求,只需迭代字符串中的字符并检查 Character.isDigit() 和/或 Character.isLetter()


J
JamisonMan111
Character first_letter_or_number = query.charAt(0);
                //------------------------------------------------------------------------------
                if (Character.isDigit())
                {

                }
                else if (Character.isLetter())
                {

                }

v
vaquar khan

工作测试示例

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

public class PaserNo {

    public static void main(String args[]) {

        String text = "gg";

        if (!StringUtils.isBlank(text)) {
            if (stringContainsNumber(text)) {
                int no=Integer.parseInt(text.trim());
                System.out.println("inside"+no);

            } else {
                System.out.println("Outside");
            }
        }
        System.out.println("Done");
    }

    public static boolean stringContainsNumber(String s) {
        Pattern p = Pattern.compile("[0-9]");
        Matcher m = p.matcher(s);
        return m.find();
    }
}

您的代码仍然可以被“1a”等中断,因此您需要检查异常

if (!StringUtils.isBlank(studentNbr)) {
                try{
                    if (isStringContainsNumber(studentNbr)){
                    _account.setStudentNbr(Integer.parseInt(studentNbr.trim()));
                }
                }catch(Exception e){
                    e.printStackTrace();
                    logger.info("Exception during parse studentNbr"+e.getMessage());
                }
            }

检查 no 的方法是不是字符串

private boolean isStringContainsNumber(String s) {
        Pattern p = Pattern.compile("[0-9]");
        Matcher m = p.matcher(s);
        return m.find();
    }

A
Adam Bodrogi

在这种典型场景中涉及任何异常抛出/处理是一种不好的做法。因此 parseInt() 不是很好,但正则表达式是一个优雅的解决方案,但请注意以下事项: -fractions -负数 -小数分隔符可能在 contries 中有所不同(例如','或'.') -有时允许有一个所谓的千位分隔符,例如空格或逗号,例如 12,324,1000.355

要处理应用程序中的所有必要情况,您必须小心,但此正则表达式涵盖了典型场景(正/负和小数,用点分隔): ^[-+]?\d*.?\d+$ < br>为了测试,我推荐regexr.com


u
user176692

Adam Bodrogi 稍作修改的版本:

public class NumericStr {


public static void main(String[] args) {
    System.out.println("Matches: "+NumericStr.isNumeric("20"));         // Should be true
    System.out.println("Matches: "+NumericStr.isNumeric("20,00"));          // Should be true
    System.out.println("Matches: "+NumericStr.isNumeric("30.01"));          // Should be true
    System.out.println("Matches: "+NumericStr.isNumeric("30,000.01"));          // Should be true
    System.out.println("Matches: "+NumericStr.isNumeric("-2980"));          // Should be true
    System.out.println("Matches: "+NumericStr.isNumeric("$20"));            // Should be true
    System.out.println("Matches: "+NumericStr.isNumeric("jdl"));            // Should be false
    System.out.println("Matches: "+NumericStr.isNumeric("2lk0"));           // Should be false
}

public static boolean isNumeric(String stringVal) {
    if (stringVal.matches("^[\\$]?[-+]?[\\d\\.,]*[\\.,]?\\d+$")) {
        return true;
    }

    return false;
}
}

今天不得不使用这个,所以刚刚发布了我的修改。包括货币、千位逗号或句点符号以及一些验证。不包括其他货币符号(欧元、美分),验证逗号是每三位数字。


S
Sandeep
public class Test{  
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String str;
    boolean status=false;
    System.out.println("Enter the String : ");
    str = sc.nextLine();
    
    char ch[] = str.toCharArray();
    
    for(int i=0;i<ch.length;i++) {
        if(ch[i]=='1'||ch[i]=='2'||ch[i]=='3'||ch[i]=='4'||ch[i]=='5'||ch[i]=='6'||ch[i]=='7'||ch[i]=='8'||ch[i]=='9'||ch[i]=='0') {
            ch[i] = 0;
        }
    }
    
    for(int i=0;i<ch.length;i++) {
        if(ch[i] != 0) {
            System.out.println("Mixture of letters and Digits");
            status = false;
            break;
        }
        else
            status = true;
    }
    
    if(status == true){
        System.out.println("Only Digits are present");
    }
}

}