0

I'm having the following string as an example:

<tr class="row_odd"><td>08:00</td><td>08:10</td><td><a href="editactivity.php?act=11111">TEST1</a></td></tr><tr class="row_even"><td>08:10</td><td>08:15</td><td><a href="editactivity.php?act=22222">TEST2</a></td></tr><tr class="row_odd"><td>08:15</td><td>08:20</td><td><a href="editactivity.php?act=33333">TEST3</a></td></tr><tr class="row_even"><td>08:20</td><td>08:25</td><td><a href="editactivity.php?act=44444">TEST4</a></td></tr><tr class="row_odd"><td>08:25</td><td>08:30</td><td><a href="editactivity.php?act=55555">TEST5</a></td></tr>

I need to have to have the output as a onedimensional Array. Like 11111=myArray(0) , 22222=myArray(1) , 33333=myArray(2) ,......

I have already tried the myString.replace, but it seems I can only replace a single Char that way. So I need to use expressions and a for loop for filling the array, but since this is my first c# project, that is a bridge too far for me.

Thanks,

  • `String.Replace` has an overload that takes two strings as arguments. Make sure you use double quotes for the string literals. (i.e. ") – vlad Apr 19 '11 at 18:00

4 Answers4

1

It seems like you want to use a Regex search pattern. Then return the matches (using a named group) into an array.

var regex = new Regex("act=\?(<?Id>\d+)");
regex.Matches(input).Cast<Match>()
     .Select(m => m.Groups["Id"])
     .Where(g => g.Success)
     .Select(g => Int32.Parse(g.Value))
     .ToArray();

(PS. I'm not positive about the regex pattern - you should check into it yourself).

Reddog
  • 15,219
  • 3
  • 51
  • 63
0

Use a combination of IndexOf and Substring... something like this would work (not sure how much your string varies). This will probably be quicker than any Regex you come up with. Although, looking at the length of your string, it might not really be an issue.

    public static List<string> GetList(string data)
    {
        data = data.Replace("\"", ""); // get rid of annoying "'s
        string[] S = data.Split(new string[] { "act=" }, StringSplitOptions.None);
        var results = new List<string>();

        foreach (string s in S)
        {
            if (!s.Contains("<tr"))
            {
                string output = s.Substring(0, s.IndexOf(">"));
                results.Add(output);
            }

        }
        return results;
    }
Ian G
  • 29,468
  • 21
  • 78
  • 92
0

Several ways you could do this. A couple are:

a) Use a regular expression to look for what you want in the string. Used a named group so you can access the matches directly
http://www.regular-expressions.info/dotnet.html

b) Split the expression at the location where your substrings are (e.g. split on "act="). You'll have to do a bit more parsing to get what you want, but that won't be to difficult since it will be at the beginning of the split string (and your have other srings that dont have your substring in them)

0

Split your string using HTML tags like "<tr>","</tr>","<td>","</td>", "<a>","</a>" with strinng-variable.split() function. This gives you list of array.

Split html row into string array

Community
  • 1
  • 1
Priyank
  • 10,503
  • 2
  • 27
  • 25