Solved

Fill a String

Posted on 2004-08-03
6
1,256 Views
Last Modified: 2013-11-20
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
0
Comment
Question by:Dj_Fx8
[X]
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
6 Comments
 
LVL 13

Expert Comment

by:SteH
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;
0
 
LVL 13

Expert Comment

by:SteH
ID: 11701256
Or

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

Expert Comment

by:AndyAinscow
ID: 11703309
CString s("      ");  //s contains 6 spaces
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 125 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);
0
 

Author Comment

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

Expert Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Login Script to Copy Folders 12 47
sum13 challenge 24 134
Annoying "thing" blocks my view 4 132
JQuery serialize and unserialize 8 193
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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 a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

749 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