GLProgramming.com

home :: about :: development guides :: irc :: forums :: search :: paste :: links :: contribute :: code dump

-> Click here to learn how to get live help <-


New Paste :: Recent Pastes:: No Line Numbers


C# Function RemoveEmptyStrings by zach
1
 
// 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
        }