Abhishek Vaidya
Abhishek Vaidya

Reputation: 102

How to extract the Starting characters from a String?

I have a column UserID, value in column starts with D, SIN, SUD example

D4568732

SIN454544

SUD4545454

How to extract this starting pattern in C#?

Upvotes: 1

Views: 80

Answers (3)

Arvind Chourasiya
Arvind Chourasiya

Reputation: 17422

Solution 1

You can try this to remove alphabets from string & result would be in digits only

 string input = "ABC12345";   
 var result = Convert.ToInt32(new String(input.Where(p => Char.IsNumber(p)).ToArray()));

Solution 2

Another possible answer would be this(Here it just skipping the alphabets from your input string)

 string input = "ABC12345";
 string result = new String(input.SkipWhile(p => Char.IsLetter(p)).ToArray());

Upvotes: 2

Neil Hu
Neil Hu

Reputation: 1

The regex apply for handing string like below

        string str = "SIN454544";

        var matches=Regex.Match(str,"^([A-Z]+)\\d+");

        var patten = matches.Groups[1].Value;

"[A-Z]+" match any english character and "d+" match any number,the "()" will fetch match value from groups

Upvotes: 0

hankor
hankor

Reputation: 400

Use a regular expression like this:

System.Text.RegularExpressions.Regex.Match("SIN454544", "^D|SIN|SUD")

Upvotes: 4

Related Questions