Parsing text from a string

Parsing a string.

I would like to get 99999  from 99999 - Local Call
zachvaldezAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Shaun VermaakTechnical 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zachvaldezAuthor Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.