0

How can I remove country code and special characters from my given phone number : +1-541-xxx-xxxx I want as a result : 541xxxxxxx

  String edt = edittext.toString();
  String num = edt.substring(Math.max(edt.length() - 10, 0));
  result = num.replaceAll("-","")

if(s.length()>10) {

                String edt = s.toString();
                System.out.println("@@"+edt);

                System.out.println("== PHONE ==:"+edt.substring(Math.max(edt.length() - 10, 0)));
                String kk = edt.substring(Math.max(edt.length() - 10, 0));
                result = kk.replaceAll("-","");

                //edtSearch.setText(kk);
                System.out.println("== PHONE ==:"+kk);

            }
Anil
  • 1,605
  • 1
  • 14
  • 24
Dolly Rosy
  • 140
  • 10
  • split on - , then join all elements starting from 1st element – Satya Aug 08 '17 at 11:18
  • What exactly you want to achieve here? You can set length limit and inputType to number in your xml, which will avoid input of special characters and will limit the maximum number of characters – Arshad Aug 08 '17 at 11:19
  • i dont want to change on xml side i just want a actual 10 digits number from formatted phone number in a string. that string i need to send to my server – Dolly Rosy Aug 08 '17 at 11:21
  • Possible duplicate of [Pick Phone Number from contacts](https://stackoverflow.com/questions/45435121/pick-phone-number-from-contacts) – Olena Y Aug 08 '17 at 11:25
  • Possible duplicate of [how to remove country code when i paste a phone number which is copied from another resource (need to remove special chars also )?](https://stackoverflow.com/questions/45475440/how-to-remove-country-code-when-i-paste-a-phone-number-which-is-copied-from-anot) – Vladyslav Matviienko Aug 08 '17 at 11:51
  • how this differs from your other question? – Vladyslav Matviienko Aug 08 '17 at 11:52

1 Answers1

2

Try this

 String number = "+1-541-8569-7896";
    String[] split = number.split("-",2);

    String num1 = split[1];
    num1=num1.replaceAll("[\\D]", "");
    Log.d("=============>>>",num1);
Anil
  • 1,605
  • 1
  • 14
  • 24
  • it removes all specail characters but country code wasnt removed with this code. here is this results is :: 154185697896 – Dolly Rosy Aug 08 '17 at 11:27