Parsing text from a string

Parsing a string.

I would like to get 99999  from 99999 - Local Call
zachvaldezAsked:
Who is Participating?
 
Shaun VermaakConnect With a Mentor Technical Specialist/DeveloperCommented:
If you cannot use LINQ
Regex regEx = new Regex(@"\d+");
Match match = regEx.Match("99999 - Local Call");

if (match.Success)
{
    Console.WriteLine(match.Value);
}
else
{
    Console.WriteLine("No number found");
}

Open in new window

0
 
Ares KurkluSoftware EngineerCommented:
You can have many ways of doing it but with some of those ways you need to be careful you need to be sure that there won't be any other numeric value after 99999  like "99999  - Local Call 1"

You can use regular expressions to ignore any non digits, or use LINQ
string s =  "99999  - Local Call";
s= new string(s.Where(x => char.IsDigit(x)).ToArray());

or if you are sure that there will be a " - " then you can find index of that and get a substring from 0 to that point

like :

string s = "99999 - Local Call";
            string a = s.Substring(0, s.IndexOf("-", 0)).TrimEnd();
0
 
Gustav BrockCIOCommented:
Or simply:

string s = "99999 - Local Call";
string n = s.Split(' ')[0];

// n -> "99999"

Open in new window

0
 
zachvaldezAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.