java 从电话号码字符串条目中删除所有特殊字符,除了 + 仅出现在第一位

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17268912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:32:15  来源:igfitidea点击:

Remove all special characters from a phone number string entry except + occurring only at first place

javaandroidregexstringreplace

提问by S P

I want to remove all special characters from a phone number string entry except + symbol. That too, if it is occurring only at first place. Example : +911234567890 should be valid but +91+1234#1234 should be invalid.

我想从电话号码字符串条目中删除除 + 符号之外的所有特殊字符。这也是,如果它只发生在第一位。例如:+911234567890 应该是有效的,但 +91+1234#1234 应该是无效的。

回答by Nargis

You can use something like:

你可以使用类似的东西:

String number = "+91+1234#1234"
number=number.replaceAll("[\D]", "")

This will replace all non digit characters with space but then for your additional "+" in the beginning ,you may need to add it as a prefix to the result.

这将用空格替换所有非数字字符,但是对于开头的附加“+”,您可能需要将其添加为结果的前缀。

Hope this helps!

希望这可以帮助!

回答by Yakiv Mospan

The best way is to use regular expression:

最好的方法是使用正则表达式:

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

public class Main
{
    public static void main(String[] args)
    {
    String sPhoneNumber = "+911234567890";

    Pattern pattern = Pattern.compile("^[+]\d*");
    Matcher matcher = pattern.matcher(sPhoneNumber);

    if (matcher.matches()) {
        System.out.println("Phone Number Valid");
    } else {
        System.out.println("Phone Number must start from  + ");
    }
 }
}

Best wishes.

最好的祝愿。

回答by user2515154

Scanner scan=new Scanner(System.in);
        String input=scan.next();
        String onlyDigits = input.replaceAll("[^0-9]+","");
        System.out.println(onlyDigits);

回答by Dulanga

Try this

试试这个

public static void main(String arg[]){

String num="+45*#545454*j";
String edited="";

for (int i=0;i<num.length();i++){

char c=num.charAt(i);
    if (i==0&&c=='+'){
        edited+=c;
    }
    else if (Character.isDigit(c)){
        edited+=c;
    }

}
System.out.println(edited);
}
}

回答by NoJoshua

Use a MaskFormatterto constrain the input, something like this

使用MaskFormatter来约束输入,像这样

    MaskFormatter formatter = null;
    try {
        formatter = new MaskFormatter("+###");
    } catch (ParseException e) {
        e.printStackTrace();
    }
    JFormattedTextField tf = new JFormattedTextField(formatter);

The string "+###" in the constructor defines what can be typed in the textfield; in this case the first character needs to be '+' and the following three needs to be digits. See the APIfor details.

构造函数中的字符串“+###”定义了可以在文本字段中输入的内容;在这种情况下,第一个字符需要是“+”,接下来的三个字符需要是数字。有关详细信息,请参阅API

回答by Sanjaya Liyanage

Here is an answer for you.But please give a try and then ask the question so that you can improve yourself. At least give a try to understand the code without directly use it

这是给您的答案。但请尝试一下,然后提出问题,以便您可以提高自己。至少尝试理解代码而不直接使用它

public static boolean isValid(String number) {

        if(number.startsWith("+")){
            number=number.substring(1);
        }
        try {
             Integer.parseInt(number);
        } catch (Exception e) {
            return false;
        }
        return true;
    }