我在应用程序中加载了一个字符串,它可以从数字变成字母等等。我有一个简单的if语句,看看它是否包含字母或数字,但是,有些东西不太正确。下面是一个片段。

String text = "abc"; 
String number; 

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

虽然文本变量包含字母,但条件返回为true。和&&应该eval作为两个条件都必须为真,以便处理number =文本;

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

解决方案:

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

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

String text = "abc"; 
String number; 

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

当前回答

为了简单地检查字符串是否只包含字母,使用以下代码:

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

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

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

希望这能帮助到一些人!

其他回答

This code is already written. If you don't mind the (extremely) minor performance hit--which is probably no worse than doing a regex match--use Integer.parseInt() or Double.parseDouble(). That'll tell you right away if a String is only numbers (or is a number, as appropriate). If you need to handle longer strings of numbers, both BigInteger and BigDecimal sport constructors that accept Strings. Any of these will throw a NumberFormatException if you try to pass it a non-number (integral or decimal, based on the one you choose, of course). Alternately, depending on your requirements, just iterate the characters in the String and check Character.isDigit() and/or Character.isLetter().

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

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

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

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

}

boolean flag = false;
        System.out.print("Enter String : ");
        String str = new Scanner(System.in).next();

        for (int i = 0; i < str.length(); i++)
        {
            if (str.length() <= 0)
            {
                System.out.println("String length Can't be zero.");
                return;
            }
            char ch = str.charAt(i);
            int c = ch;
            if (c >= 48 && c <= 58)
            {
                flag = true;
            } else
            {
                flag = false;
                break;
            }
        }
        if (flag)
        {
            System.out.println("input [" + str + "] contains number only.");
        } else
            System.out.println("input [" + str + "] have some non string values in it.");