Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Fill a String

Posted on 2004-08-03
Medium Priority
Last Modified: 2013-11-20

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
Question by:Dj_Fx8
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 13

Expert Comment

ID: 11701237
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;
LVL 13

Expert Comment

ID: 11701256

void Fill  (CString& str, char cFill, int nCount)
    char* cs = str.GetBufferSetLength (nCount);
    memset (cs, cFill, nCount);
    str.ReleaseBuffer (nCount);
LVL 45

Expert Comment

ID: 11703309
CString s("      ");  //s contains 6 spaces
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 55

Accepted Solution

Jaime Olivares earned 500 total points
ID: 11705331
To construct directly, just use:

CString s(' ', 6);

If you want to assign to another string:

CString s = "whatever";
s = CString(' ', 6);

Author Comment

ID: 11707071
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
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11708817
Those points are walking too slow. ;-P
Thanks anyway!

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question