英文:
Validate if string has only numbers
问题
My Java application 'A' is getting mobile number as String from another java application. So after app A gets the mobile number string, I need to validate if that mobile number String has only numbers in it. For validating that I have used a simple logic as below,
public static boolean containsOnlyDigits(String str, int n) {
for (int i = 1; i < n; i++) {
if (!Character.isDigit(str.charAt(i))) {
return false;
}
}
return true;
}
I am checking from i=1 as 1st character will be '+' for country code. This approach is O(n). There is another approach where we can use Double.parseDouble(str). This will throw NumberFormatException so that we can catch and consider it is an Alphanumeric String.
Which of these approaches are best based on performance?
英文:
My Java application 'A' is getting mobile number as String from another java application. So after app A gets the mobile number string, I need to validate if that mobile number String has only numbers in it. For validating that I have used a simple logic as below,
public static boolean containsOnlyDigits(String str, int n) {
for (int i = 1; i < n; i++) {
if (!Character.isDigit(str.charAt(i))) {
return false;
}
}
return true;
}
I am checking from i=1 as 1st character will be '+' for country code. This approach is O(n). There is another approach where we can use Double.parseDouble(str)
. This will throw NumberFormatException
so that we can catch and consider it is an Alphanumeric String.
Which of these approaches are best based on performance?
答案1
得分: 1
你可以尝试,如果没有用处可以去掉 +
符号:
/**
* 假设电话号码采用国际格式,前缀带有 `+` 号,并且数字之间没有空格,验证字符串是否为有效电话号码。
*
* @param phone
* @return 结果布尔值
*/
private boolean isValidPhoneNumber(String phone) {
if (phone == null) return false;
return Pattern.matches("\\+\\d{11,15}", phone);
}
英文:
You could try, remove the +
if not useful:
/**
* Assuming that a phone number is of an international format, having `+` sign as prefix
* and with no spaces in between the numbers, validates if a string is a valid phone number.
*
* @param phone
* @return resulting boolean
*/
private boolean isValidPhoneNumber(String phone) {
if(phone == null) return false;
return Pattern.matches("\\+\\d{11,15}", phone);
}
答案2
得分: 0
// 我从 i=1 开始检查,因为第一个字符将是国家/地区代码前的 '+' 号。
您可以使用正则表达式 `\+\d{n - 1}`,其中 `\+` 表示第一个字符是 '+',而后面的 `n - 1` 个字符都是数字。这也意味着字符串的总长度应为 `n`。
public class Main {
public static void main(String[] args) {
// 测试
System.out.println(containsOnlyDigits("+123456789", 10));
System.out.println(containsOnlyDigits("+123456789", 9));
System.out.println(containsOnlyDigits("+123456789", 8));
System.out.println(containsOnlyDigits("-123456789", 9));
System.out.println(containsOnlyDigits("123456789", 9));
System.out.println(containsOnlyDigits("ABC123456", 9));
}
public static boolean containsOnlyDigits(String str, int n) {
String regex = "\\+\\d{" + (n - 1) + "}";
return str.matches(regex);
}
}
**输出:**
true
false
false
false
false
false
英文:
> I am checking from i=1 as 1st character will be '+' for country code.
You can use the regex, \+\d{n - 1}
which means the first character is +
and the remaining n - 1
characters are digits. It also means that the total length of the string should be n
.
public class Main {
public static void main(String[] args) {
// Tests
System.out.println(containsOnlyDigits("+123456789", 10));
System.out.println(containsOnlyDigits("+123456789", 9));
System.out.println(containsOnlyDigits("+123456789", 8));
System.out.println(containsOnlyDigits("-123456789", 9));
System.out.println(containsOnlyDigits("123456789", 9));
System.out.println(containsOnlyDigits("ABC123456", 9));
}
public static boolean containsOnlyDigits(String str, int n) {
String regex = "\\+\\d{" + (n - 1) + "}";
return str.matches(regex);
}
}
Output:
true
false
false
false
false
false
答案3
得分: 0
使用正则表达式的成本很高。因此,您可以在Java 8中使用lambda符号轻松解决这个问题。
boolean numericControl = str.chars().allMatch(x -> Character.isDigit(x));
英文:
Using regular expressions is costly. For this reason, you can easily solve this problem using the lambda notation in Java 8.
boolean numericControl = str.chars().allMatch(x -> Character.isDigit(x));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论