// Takes an array of strings and removes any empty element
public void RemoveEmptyStrings(ref string[] arrStr)
{
string[] arrStrTmp;
int nCount, nUsed;
// Find the number of non-empty strings
nCount = nUsed = 0;
for(int i = 0; i < arrStr.Length; i++)
{
if(arrStr[i] != "")
nCount++;
}
// Allocate the memory and copy over the non-empty strings
arrStrTmp = new string[nCount];
for(int i = 0; i < arrStr.Length; i++)
{
if(arrStr[i] != "")
arrStrTmp[nUsed] = arrStr[i];
}
// Assign the old array object to the new array
arrStr = arrStrTmp;
// Garbage collector will reclaim the local memory for arrStrTmp
} |