Multiplying a string by an integer returns integers?

So, I tried to create an asterisk pyramid using D. First of all, I noticed that concatenation seems impossible. Writing off something like this writeln("foo" + "bar")will give you a syntax error. So instead, I tried to multiply strings, as in python, which did not work with double quotes, but something strange happens with single quotes of a string.

If you enter this

import std.stdio;
void main()
{
    foreach (i; 0 .. 10)
    {
        writeln(i*'0');
    }
}

it will return a chain of integers. Can anyone explain why this is happening? And letting me know how to concatenate strings will also be very helpful.

thank!

+4
source share
2 answers

"0" , , ASCII. . , ASCII 'A' 65.

import std.stdio;
int main()
{
        writeln( cast(int)'A' );
        writeln( 10 * 'A' );
        return 0;
}

65 650, .

, ~ ~ "array1 ~ = array2" 2 1 .

+5

, :

char[5] arr3 = 's';
writeln(arr3);

: std.array.replicate std.range.repeat:

import std.array;
import std.stdio;

void main() {
    auto arr = replicate(['s'], 5); // lazy version: http://dlang.org/phobos/std_range.html#repeat
    // or
    auto arr2 = ['s'].replicate(5);
    writeln(arr);
    writeln(arr2);
}
+1

Source: https://habr.com/ru/post/1537452/


All Articles