How to take numbers from a string in C#

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.Match(myString , @"\d+").Value;
[/csharp]

Once you done and Run, you will get value 1 in the variable myString

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

Kindest Regards
Sibeesh Venu

Leave a Reply

Your email address will not be published. Required fields are marked *

Serverless 360