Fill a String

Hi

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
Dj_Fx8Asked:
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);
0
 
SteHCommented:
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;
0
 
SteHCommented:
Or

void Fill  (CString& str, char cFill, int nCount)
{
    char* cs = str.GetBufferSetLength (nCount);
    memset (cs, cFill, nCount);
    str.ReleaseBuffer (nCount);
}
0
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
0
 
Dj_Fx8Author Commented:
Hi
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
0
 
Jaime OlivaresSoftware ArchitectCommented:
Those points are walking too slow. ;-P
Thanks anyway!
0
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.