![]() |
| | #1 |
| Registered User Join Date: Jun 2006
Posts: 5
| Help creating function - 2005 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;
}
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];
}
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 | |
| | #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();
}
Code: string test = "thisisisSa<b>thehe</b> test<b>numba</b><b>ththhhhe";
__________________ Rule #1: Every rule has exceptions |
| pianorain is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |