www.digitalmars.com         C & C++   DMDScript  

D - constructing char[] variables

reply Andrew Edwards <remove_ridimz remove_yahoo.com> writes:
Give the following:

char[] str;
for(int i = 0; i < 50; i++)
   str ~= "*";

is there another way to accomplish this in D?
In C++ I would simply:

std::string str(50, '*');

TIA,
Andrew
-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Mar 14 2004
next sibling parent "Phill" <phill pacific.net.au> writes:
You can do it this way as well although
it is probably more hassle:

char[] another ;

    for(int i = 0; i < 50; i++)
    {
        another.length = i + 1;
     strcat(another, "*");
     }

You would need to :

import std.string;

Phill.


"Andrew Edwards" <remove_ridimz remove_yahoo.com> wrote in message
news:opr4v5dnvus6zaqn news.digitalmars.com...
 Give the following:

 char[] str;
 for(int i = 0; i < 50; i++)
    str ~= "*";

 is there another way to accomplish this in D?
 In C++ I would simply:

 std::string str(50, '*');

 TIA,
 Andrew
 --
 Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Mar 15 2004
prev sibling next sibling parent Vathix <vathix dprogramming.com> writes:
Andrew Edwards wrote:
 Give the following:
 
 char[] str;
 for(int i = 0; i < 50; i++)
   str ~= "*";
 
 is there another way to accomplish this in D?
 In C++ I would simply:
 
 std::string str(50, '*');
 
 TIA,
 Andrew
char[] str = new char[50]; str[] = '*'; -- Christopher E. Miller
Mar 15 2004
prev sibling parent J Anderson <REMOVEanderson badmama.com.au> writes:
Andrew Edwards wrote:

 Give the following:

 char[] str;
 for(int i = 0; i < 50; i++)
   str ~= "*";
Just a small quibble with this code: char[] str; str.length = 50; //Initialize size first foreach(inout char c; str) c = '*';
 is there another way to accomplish this in D?
 In C++ I would simply:

 std::string str(50, '*');
Make your own function to do this. -- -Anderson: http://badmama.com.au/~anderson/
Mar 15 2004