Results 1 to 3 of 3

Thread: [2.0] Help creating function [resolved]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    116

    Resolved [2.0] Help creating function [resolved]

    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[1];
            }
    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
    Last edited by cx323; Jun 10th, 2006 at 09:19 PM.

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: [2.0] Help creating function

    Code:
    		private void button2_Click(object sender, EventArgs e)
    		{
    			string[] testing = new string[1];
    			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];
    		}
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    116

    Resolved Re: [2.0] Help creating function

    Edit- i have it working now
    Last edited by cx323; Jun 10th, 2006 at 09:18 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width