Fill a String


Is there a way to fill a string with say 6 spaces ie
CString str= Fill(6, " ")
or do I have to write it myself
Who is Participating?
Jaime OlivaresConnect With a Mentor Software ArchitectCommented:
To construct directly, just use:

CString s(' ', 6);

If you want to assign to another string:

CString s = "whatever";
s = CString(' ', 6);
It would be easier for a char* using memset. Add one additional space which you replace with the terminating 0 afterwards:

char* cs = new char[7];
memset (cs, ' ', 7);
*cs[6] = '0x0';

CString str = cs;
delete [] cs;

void Fill  (CString& str, char cFill, int nCount)
    char* cs = str.GetBufferSetLength (nCount);
    memset (cs, cFill, nCount);
    str.ReleaseBuffer (nCount);
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

AndyAinscowFreelance programmer / ConsultantCommented:
CString s("      ");  //s contains 6 spaces
Dj_Fx8Author Commented:
Thanks for all your replies, I'm will award the points to Jaime as his answer is a simple one line of code and best answers my question
Jaime OlivaresSoftware ArchitectCommented:
Those points are walking too slow. ;-P
Thanks anyway!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.