Removal of characters from a string
In this recipe, we will see how special characters and blanks may be removed from a text string comprising of a telephone number. We will create a program that will take as input the number containing blank spaces and special characters such as +
, (
, and )
.
The replace
statement along with suitable regular expressions will be used. Various regular expressions may work in this case. We will see two such expressions in this recipe.
How to do it...
For meeting the mentioned requirement, proceed as follows:
Declare a parameter by the name
number
, consisting of20
characters.The
replace all occurrences
is added having the regular expression[^\d]
.
How it works...
The solution is based on searching all non-digit characters in the string and replacing them with blank. The negated operator (^
) is used within the box brackets and the \d
denotes the digits. We have used all occurrences, as this will replace all non-digits.
Suppose the user enters the number having +
and...