Jot Dhaliwal
Jot Dhaliwal

Reputation: 1500

Find phone number from given string c#

I have one resume , i want to find user's contact number(Mobile no or Phone no) from the resume, Need any idea or solution or any assistance for achieving the goal .

What i Have tried so far....

var numString = "";
        string strData = ",38,,,,,,,,,,,,,,,,,,,,,,,,,,,,382350,,,,,0,,,,8141884584,,,,,,,,";
        char[] separator = new char[] { ',' };
        string[] strSplitArr = strData.Split(separator);
        for (int q = 0; q < strSplitArr.Length; q++)
        {
            if (strSplitArr[q] != "")
            {
                int no = 0;

                no = strSplitArr[q].Length;
                if (no >= 10 && no <= 12)
                {
                    numString += strSplitArr[q].ToString() + ", ";
                }
            }
        }

Upvotes: 2

Views: 4372

Answers (1)

S.Dav
S.Dav

Reputation: 2466

I would suggest that you use Regular Expression

Here is a sample code to find US Phone numbers:

string text = MyInputMethod();
const string MatchPhonePattern =
       @"\(?\d{3}\)?-? *\d{3}-? *-?\d{4}";

        Regex rx = new Regex(MatchPhonePattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);

        // Find matches.
        MatchCollection matches = rx.Matches(text);

        // Report the number of matches found.
        int noOfMatches = matches.Count;


        //Do something with the matches

        foreach (Match match in matches)
        {
            //Do something with the matches
           string tempPhoneNumber= match.Value.ToString(); ;

         }

Upvotes: 3

Related Questions