0

How do you get this string from this data? (The bolded part is what I want)

Id: 20 - Cow Amt: **10**

Id: 25 - Example Amt: **5**

I made this code to remove everything else except for the ID number

id = id.Replace("Id: ", "")

If (id.ToString.Length > 5) Then
    id = id.Substring(0, id.IndexOf("-") - 1).Trim()

But now I want to also get the Amt: number as well. The outcome of the code so far is this:

20

It just prints out the id. But how do you get the 10? So it would look like this:

20 10

svick
  • 236,525
  • 50
  • 385
  • 514
TopRS
  • 117
  • 2
  • 8
  • what about a simple loop? find where the 'A' of "Amt" is and print from there. – elyashiv Aug 18 '12 at 18:00
  • what's the meaning of `Cow` in this data? Your string doesn't seem to have a specific rule – codingbiz Aug 18 '12 at 18:09
  • 1
    I'd recommend going for regular expressions matching only numbers (thus, you'd get 2 matches: 20 and 10; though it may change if your unnecessary text will contain numbers also). Here's SO post: http://stackoverflow.com/questions/273141/regex-for-numbers-only – Dmitry Reznik Aug 18 '12 at 18:12
  • Data cow is just an example. I'll take a look for regex. – TopRS Aug 18 '12 at 18:17
  • the tag and the title a confusing... are you asking for vb.net or c# or don't you care as long as it is in the .net family? – psubsee2003 Aug 18 '12 at 18:51
  • What the... I never tagged it to be c#. but anyways its fixed and – TopRS Aug 19 '12 at 19:03

1 Answers1

2

Try the following

var subjectString = "Id: 20 - Cow Amt: 10";

string[] values = Regex.Split(subjectString, @"\D+");

foreach (string value in values)
{
    int number;
    if (int.TryParse(value, out number))
    {
        Console.WriteLine(value);
    }
}

Remeber to include using System.Text.RegularExpressions; in the references at the top

Conrad Lotz
  • 8,200
  • 3
  • 23
  • 27