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;