Convert array/list to string using a separator in C++ (MFC)



Following is the code to convert array to string using a specific separator. Language used is C++ using MFC (Microsoft Foundation Classes).

This function can trim each term in the array and also ignore blank (empty) string.

You can easily modify it for list or any other similar collection class.

void ArrayToString(CAtlArray<CString> & arrValue, CString & strValue, CString strSeparator
					, BOOL bTrim/*=TRUE*/, BOOL bIgnoreEmptyString/*=TRUE*/)
{
	int i;
	strValue.Empty();
	CString strItem;
 
	//append all strings with ; as separator
	for(i=0;i<int(arrValue.GetCount());i++)
	{
		strItem = arrValue[i];
 
		if (bTrim)
		{
			strItem.Trim();
		}
 
		if (bIgnoreEmptyString)
		{
			if (strItem.IsEmpty())
			{
				continue;
			}
		}
 
		strValue += strItem;
 
 
		if (i < (int(arrValue.GetCount())-1))
		{
			strValue += strSeparator;
		}
 
	}
 
}

Leave a Reply

Your email address will not be published. Required fields are marked *