我在应用程序中加载了一个字符串,它可以从数字变成字母等等。我有一个简单的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;
}
亚当·博德罗吉的略有修改版本:
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;
}
}
今天不得不使用这个,所以刚刚发布了我的修改。包括货币、千位逗号或句号符号,以及一些验证。不包括其他货币符号(欧元、分),验证逗号为每三位数。
工作测试示例
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();
}
你可以使用正则表达式。匹配
if(text.matches("\\d*")&& text.length() > 2){
System.out.println("number");
}
或者你可以使用像Integer.parseInt(String)或更好的Long.parseLong(String)这样的onversions来获得更大的数字
比如:
private boolean onlyContainsNumbers(String text) {
try {
Long.parseLong(text);
return true;
} catch (NumberFormatException ex) {
return false;
}
}
然后用:
if (onlyContainsNumbers(text) && text.length() > 2) {
// do Stuff
}
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().