trim a string

I need to trim a string. What I need to trim off is anything following the first number(s)

for example

1 black cat jumped false true 1
12 red cat sat true false 2
123 Yellow dog sat true false 2

So all I want is the 1, 12 or 123.

How do I go about doing this. So basically anything following the first number. I can't set a length for the number becasue it is incremented based on the number of entries the user adds on another form.
LVL 2
NewMom2BrandonAsked:
Who is Participating?
 
GENTPCommented:
If then is a space always after the number then use the substring method

string myString = "1 black cat jumped false true 1";

int myNumber = Convert.ToInt32(myString.Substring(0,myString.IndexOf(" ")));
0
 
NewMom2BrandonAuthor Commented:
Thank you!!
0
 
Fernando SotoRetiredCommented:
Hi NewMom2Brandon;

This solution uses Regular Expressions. The variable will have the results, the first digits in the string.

using System.Text.RegularExpressions;

      string input = "1 black cat jumped false true 1";
      Match m = Regex.Match(input, @"\s*?(\d+)");
      string results = m.Groups[1].Value;


Fernando
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.