3

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

5
  • 2
    Sorry please can you clarify. You have a string like "123" and you want to extract all of the numbers into an int? Commented Dec 17, 2010 at 9:45
  • He clearly states the string is "1-3 of 3 Trip". (the question doesn't show if it's edited) Commented Dec 17, 2010 at 9:48
  • 1
    For non native speakers: what is a "1-3 of 3 Trip"? can you give an example? Commented Dec 17, 2010 at 9:54
  • 1
    @k3b - As a native speaker, I'm also kinda curious. Commented Dec 17, 2010 at 14:05
  • The string is (x-y of y trip) and i'm trying to get the "y" before trip i hope my explanation is clear Commented Dec 18, 2010 at 5:07

6 Answers 6

12

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('٢')
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks this works :) butcan you explain this part .Split(' ')[2]
@someguy - input.split splits the string on a space in this case (' ') and returns an arrray of strings that are found between the spaces in the source string. The [2] of course then indexes the 3rd string in the array which is the number 3 in the OP's question.
2

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]);

Comments

1

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;
}

Comments

1

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

Comments

0

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]);
}

2 Comments

tripEntry would be '1-3 of 3 Trip': therefore the return value is 1, but it should be 3 (the second one)
@PowerRoy - this has become apparent, but wasn't overly clear when I answered, sorry. For the case you mention, this obviously doesn't work.
0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.