someguy
someguy

Reputation: 1035

How to get number from string in C#

i have a String in HTML (1-3 of 3 Trip) how do i get the number 3(before trip) and convert it to int.I want to use it as a count

Found this code

public static string GetNumberFromStr(string str)
{
  str = str.Trim();
  Match m = Regex.Match(str, @"^[\+\-]?\d*\.?[Ee]?[\+\-]?\d*$");
  return (m.Value);
}

But it can only get 1 number

Upvotes: 3

Views: 19113

Answers (6)

Tohid
Tohid

Reputation: 6679

Another way to do it:

public static int[] GetNumbersFromString(string str)
{
   List<int> result = new List<int>();
   string[] numbers = Regex.Split(input, @"\D+");
   int i;

   foreach (string value in numbers)
   {
      if (int.TryParse(value, out i))
      {
         result.Add(i);
      }
   }

   return result.ToArray();
}

Example of how to use:

const string input = "There are 4 numbers in this string: 40, 30, and 10.";
int[] numbers = MyHelperClass.GetNumbersFromString();

for(i = 0; i < numbers.length; i++)
{
    Console.WriteLine("Number {0}: {1}", i + 1, number[i]);
}

Output:

Number: 4

Number: 40

Number: 30

Number: 10

Thanks to: http://www.dotnetperls.com/regex-split-numbers

Upvotes: 1

Saeb Amini
Saeb Amini

Reputation: 24400

Regex is unnecessary overhead in your case. try this:

int ExtractNumber(string input)
{
    int number = Convert.ToInt32(input.Split(' ')[2]);
    return number;
}

Other useful methods for Googlers:

// throws exception if it fails
int i = int.Parse(someString);

// returns false if it fails, returns true and changes `i` if it succeeds
bool b = int.TryParse(someString, out i);

// this one is able to convert any numeric Unicode character to a double. Returns -1 if it fails
double two = char.GetNumericValue('٢')

Upvotes: 12

Jason Evans
Jason Evans

Reputation: 29186

Try this:

public static int GetNumberFromStr(string str)
{
    str = str.Trim();
    Match m = Regex.Match(str, @"^.*of\s(?<TripCount>\d+)");

    return m.Groups["TripCount"].Length > 0 ? int.Parse(m.Groups["TripCount"].Value) : 0;
}

Upvotes: 1

James Lawruk
James Lawruk

Reputation: 31345

Forget Regex. This code splits the string using a space as a delimiter and gets the number in the index 2 position.

string trip = "1-3 of 3 trip";
string[] array = trip.Split(' ');
int theNumberYouWant = int.Parse(array[2]);

Upvotes: 2

cyberzed
cyberzed

Reputation: 2076

Not really sure if you mean that you always have "(x-y of y Trip)" as a part of the string you parse...if you look at the pattern it only catches the "x-y" part thought with the acceptance of .Ee+- as seperators. If you want to catch the "y Trip" part you will have to look at another regex instead.

You could do a simple, if you change the return type to int instead of string:

Match m = Regex.Match(str, @"(?<maxTrips>\d+)\sTrip");
return m.Groups["maxTrips"].Lenght > 0 ? Convert.ToInt32(m.Groups["maxTrips"].Value) : 0;

Upvotes: 0

Paddy
Paddy

Reputation: 33857

If I'm reading your question properly, you'll get a string that is a single digit number followed by ' Trip' and you want to get the numeric value out?

public static int GetTripNumber(string tripEntry)
{
    return   int.Parse(tripEntry.ToCharArray()[0]);
}

Upvotes: 0

Related Questions