Removing non-numeric characters

4
A phone number needs to have non-numeric characters removed. For example, change 0031 (010)-9876543 into 00310109876543. How to do this in microflow?
asked
2 answers
8

I could think of the following solutions:

  1. Make sure users cannot enter phone numbers in another format then the numeric format by using regular expression validation and/or an input mask. Do not use an integer value here, because that will remove the zeros in front of the phone number.

  2. Write a Java action that returns the phone number without non-numeric characters.

  • Arguments: PhoneNumberAllChars (String)
  • return value: String

Example code:

// BEGIN USER CODE
String tmpPhoneNumber = "";
for(int x=0;x<PhoneNumberAllChars.length();x++)
{
    int tmpCharNr = (int)PhoneNumberAllChars.charAt(x);
    if(tmpCharNr>47&&tmpCharNr<58)
    {
        tmpPhoneNumber += (char)tmpCharNr;
    }
}
if(tmpPhoneNumber.length()==0) return null;
else return tmpPhoneNumber;
// END USER CODE
answered
2

In general, it is easier (well, if you have little experience with them) and faster to use regular expressions for this kind of string manipulations. See for among other this example.

The oneliner is (untested):

String result =  Pattern.compile("[^\\d]+").matcher(originalvalue).replaceAll("");

The regular expression [^\d]+ means: match any sequence (+) of characters ([...]) which are not (^) digits (\d) (the second slash in the regex is required to escape the first one)

answered