Have you ever faced the problem where you have a String that contains white space and would like to remove or replace it, e.g (00054 675 286).
Using the
Replace()
method, you can easily replace this space with any character you like, or if you need to totally remove it.
The first example shows the removal of the white space completely.
string mobileNumber = "00054 675 286"
mobileNumber = mobileNumber.Replace(" ",")
Output = 00054675286
The second example replaces the spaces with any charecter, for this example, we will be using the (/) character.
string mobileNumber = "00054 675 286"
mobileNumber = mobileNumber.Replace(" ","/")
Output = 00054/675/286
By using the
Replace()
method, you can replace any character you would like in a string with any other character.
Hope this simple but effective tip helps in the future.
- Nick Austin