string initialization question.

Justin Spahr-Summers Justin.SpahrSummers at gmail.com
Fri Jul 30 08:46:32 PDT 2010


On Fri, 30 Jul 2010 11:35:15 -0400, Steven Schveighoffer 
<schveiguy at yahoo.com> wrote:
> 
> On Fri, 30 Jul 2010 11:24:41 -0400, dcoder <dcoder at devnull.com> wrote:
> 
> > Hello.
> >
> > Is there anyway in D to convenient fill a string variable with a char  
> > say X times?
> >
> > So, I'd like to do something like:
> >
> > string divider( size, '-');    // C++ notation.
> > $divider = '-' x $size;        // perl notation.
> >
> >
> > I thought I could do the following:
> >
> > const char divider[rowstr.length] = '-';
> >
> > but the compiler complains about not having a constant integer  
> > expression.
> >
> > thanks.
> 
> It's most likely complaining about rowstr.length not being a constant, not  
> the '-'.  This works:
> 
> const char divider[5] = '-';
> 
> If you want to allocate a new array on the heap with '-' in it, I think  
> there is a way, but I'm not sure how to do it.  I'm pretty sure there's a  
> runtime function to do it.
> 
> -Steve

Something like this will work on the heap:

    char[] divider = new char[5];
    divider[] = '-';


More information about the Digitalmars-d-learn mailing list