简体   繁体   English

我需要写一个仅包含0到9之间的数字的字符串

[英]I need to write a string that contains only numbers from 0 to 9

So i have this project where i need to write a mutator method that changes a person's phone number only if the phone number contains numbers from 0 to 9 and no letters or any other things. 因此,在这个项目中,我需要编写一个mutator方法,仅当电话号码包含从0到9的数字且没有字母或其他任何东西时,该方法才能更改一个人的电话号码。 The type of the phone number is string and i tried using conditional statements. 电话号码的类型是字符串,我尝试使用条件语句。 If the mobile contains anything other than numbers then nothing should happen. 如果移动电话包含数字以外的任何内容,则什么也不会发生。 this is what i got so far: 这是我到目前为止所得到的:

public void setMobile(String mobile) {
if(mobile.matches("[a-zA-z]+")){
 }
 else{      
        this.mobile = mobile;
 }

}

thanks 谢谢

您的正则表达式模式应为[0-9]+

If you are looking for anything that isn't a number then the pattern is \\D . 如果您要查找的不是数字,则模式为\\D That just means non-digit and is a shorthand for [^0-9] 那只是意味着非数字,是[^0-9]的简写

See http://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html 参见http://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何编写仅在字符串包含某些字符和数字时才能激活的正则表达式? - How can I write a regular expression that is only activated if the string contains certain characters and numbers? 我需要编写一个特殊字符的Java字符串和从0到9的数字来验证密码 - i need to write a java string of Special Character and numbers from 0 to 9 to validate a password 我需要编写只返回整数的幂的方法 - I need to write method that return power of only integer numbers 如何验证文本框中的字符串仅包含2个字符和3个数字? - How to validate that a string from a textbox contains only 2 characters and 3 numbers? 我需要从Java中的String中提取数字 - I need to extract numbers from a String in Java 如何通过添加仅包含 0 和 1 的数字来生成字符串 - How to generate a string by adding the numbers that only contains 0 & 1 如何遍历字符串以确保仅在某些位置包含数字? - How do I go through the String to make sure it only contains numbers in certain places? 我需要一个正则表达式,它只接受一个字符串(仅包含字母和数字),并且在开始和结尾处都允许有空格,但不能在两者之间插入空格? - I need a Regex, that which accepts only a String(with only Alphabets and Numbers) with spaces allowed at the start and end, but not in between? Java String - 查看字符串是否仅包含数字和字符而不包含单词? - Java String - See if a string contains only numbers and characters not words? Java String - 查看字符串是否只包含数字而不包含字母 - Java String - See if a string contains only numbers and not letters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM