Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1312
  • Last Modified:

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
0
Dj_Fx8
Asked:
Dj_Fx8
1 Solution
 
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
 
AndyAinscowCommented:
CString s("      ");  //s contains 6 spaces
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Jaime OlivaresSoftware ArchitectCommented:
To construct directly, just use:

CString s(' ', 6);

If you want to assign to another string:

CString s = "whatever";
s = CString(' ', 6);
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now