Thread: Help creating function - 2005

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    5

    Help creating function - 2005

    This is probably a stupid error since i'm new to c# but i can't figure it out.

    Code:
    public static int GrabAllBetween(string strSource, string strStart, string strEnd, ref string[] strArray)
            {
                int iPos;
                int iEnd;
                int iCount = 0;
                string[] Matches = new string[100];
    
                iPos = strSource.IndexOf(strStart, 1);
                while (iPos > 0)
                {
                    iEnd = strSource.IndexOf(strEnd, iPos);
                    if (iEnd > 0)
                    {
                        Matches[iCount] = strSource.Substring((iPos + strStart.Length), iEnd - iPos - (strEnd.Length - 1));
                        iPos = strSource.IndexOf(strStart, iEnd);
                    }
                    else
                    {
                        Matches[iCount] = strSource.Substring(iPos);
                        iPos = 0;
                    }
                    iCount++;
                    if (iCount > Matches.Length)
                    {
                        string[] tmp = new string[iCount + 100];
                        Matches.CopyTo(tmp, 0);
                        Matches = tmp;
                    }
                }
                if (iCount > 0)
                {
                    string[] tmp = new string[iCount - 1];
                    System.Array.Copy(Matches,iCount - 1,tmp,0,iCount-1); 
                    strArray = tmp;
                }
                return iCount;
            }
    Thats a function i created to grab all instances of a string between two other strings and put the results in an array. This is how i call it to test:

    Code:
     private void button2_Click(object sender, EventArgs e)
            {
       string[] testing = new string[0];
                string test = "thisisisSa<b>thehe</b> test<b>numba</b><b>ththhhhe</b>";
                label1.Text = GrabAllBetween(test, "<b>", "</b>", ref testing).ToString();
                label2.Text = testing[0];
            }
    Label1 shows 3, but label2 stays blank. I think the problem is with redimming the array in the function. I looked up how to redim preserve in c# on google, but i'm not sure if i completely understand it. Thanks for any help

    Edit- now i have it displaying the third item in label2 when it should be displaying the first.

    Edit - fixed it
    Code:
     string[] tmp = new string[iCount + 1];
                    System.Array.Copy(Matches, tmp, iCount + 1);
                    strArray = tmp;
    Last edited by cx323; 06-10-2006 at 08:19 PM.

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Why are you bothering with the ref parameter and the hackish redim'ing of an array? Use some real .Net data structures; that's why they're there. Don't be afraid to return an array.
    Code:
    public static string[] GrabAllBetween(string strSource, string strStart, string strEnd)
    {
        List<string> Matches = new List<string>();
    
        for (int pos = strSource.IndexOf(strStart, 0),
            end = pos >= 0 ? strSource.IndexOf(strEnd, pos) : -1;
            pos >= 0 && end >= 0;
            pos = strSource.IndexOf(strStart, end),
            end = pos >= 0 ? strSource.IndexOf(strEnd, pos) : -1)
        {
            Matches.Add(strSource.Substring(pos + strStart.Length, end - pos - strEnd.Length + 1));
        }
    
        return Matches.ToArray();
    }
    Note that your current version will fail for the following test string:
    Code:
    string test = "thisisisSa<b>thehe</b> test<b>numba</b><b>ththhhhe";
    Your function will find three strings when only two strings are really enclosed by the bold tag.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM