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);
Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

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.