tags:

views:

67

answers:

1

How can I extract the decimal part of a string that has an equals sign as the delimiter?

Example:

 2 = No
10 = (6 - 8 hrs/day, Good & Restful)
1 = low in fat 1 = low in sugar 1 = high in fiber

Someone please help. Thanks.

+2  A: 

The following C# code will return number that is located in the left of the equal sign into an integer list given a string "input":

// string input = "<your input>";
Match m = Regex.Match(input, @"\s*(?<dec>\d+)\s*=");
List<int> intList = new List<int>();

while (m.Success)
{
    intList.Add(Int32.Parse(m.Groups["dec"].Value));
    m = m.NextMatch();
}

// Process intList
Frank Liao
+1 but you might want to consider using TryParse instead of Parse so that the program doesn't break on invalid input.
Mark Byers