Site icon Sibeesh Passion

How to replace number from a string in C#

In this pose we will see how to replace a number from a string in C#. We can do this in several ways. Here I am going to show you one. I hope you will like this.

Consider following is my string.

[csharp]
String myString = "MyString1";
[/csharp]

And I need to take out the number 1 from the value MyString1. What will we do?

To do this we can use Regex class in C#. Following is the code to achieve the same ๐Ÿ™‚

[csharp]
myString = Regex.Replace(sortdatafield, @"[\d-]", string.Empty);
[/csharp]

Once you done and Run, you will get the new value without numbers (โ€œMyStringโ€) in the variable myString

I hope someone found this useful. Happy Coding ๐Ÿ™‚

Kindly see my code snippets here

Kindest Regards
Sibeesh Venu

Exit mobile version