C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-10-2006, 07:34 PM   #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.
cx323 is offline   Reply With Quote
Old 06-10-2006, 09:31 PM   #2
Anti-Poster
 
Join Date: Feb 2002
Posts: 1,241
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.
__________________
Rule #1: Every rule has exceptions
pianorain is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Which Library Files for these unreferenced functions? lehe C++ Programming 3 01-31-2009 10:30 PM
Troubleshooting Input Function SiliconHobo C Programming 14 12-05-2007 07:18 AM
Please Help - Problem with Compilers toonlover C++ Programming 5 07-23-2005 10:03 AM
Question.. pode Windows Programming 12 12-19-2004 07:05 PM
c++ linking problem for x11 kron Linux Programming 1 11-19-2004 10:18 AM


All times are GMT -6. The time now is 11:04 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22