www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How can I concatenate a string, a char array and an int

reply Anders S <anders xore.se> writes:
Hi guys,

just started to get into Dlang, comming from C and C++ I like to 
use methods like there if possible.

Now I want to catenate something like this, but don't get it to 
work
in standard C i code:
    char str[80];
    sprintf(str, "This is a number = %f", 3.14356);

Now in Dlang and import core.stdc.string and
code:
     char [80] str;
     sprintf(str, "This is a number = %d", 314356);
     writefln("%s", str);

but get error
Error: function core.stdc.stdio.sprintf (char* s, const(char*) 
format, ...) is not callable using argument types (char[80], 
string, int)

Nor does this work
    char [50] temp = "This is a number";
    string greeting5= temp~" "~314356;
    writefln("%s",greeting5);

result in error:
Error: incompatible types for ((cast(const(char)[])temp ~ " ") ~ 
(314356)): 'char[]' and 'int'


Any ideas or hints?
/anders
Nov 29 2016
next sibling parent reply rumbu <rumbu rumbu.ro> writes:
On Tuesday, 29 November 2016 at 10:21:24 UTC, Anders S wrote:
 Hi guys,

 just started to get into Dlang, comming from C and C++ I like 
 to use methods like there if possible.

 Now I want to catenate something like this, but don't get it to 
 work
 in standard C i code:
    char str[80];
    sprintf(str, "This is a number = %f", 3.14356);
import std.format; string str = format("This is a number = %f", 3.14356);
 Now in Dlang and import core.stdc.string and
 code:
     char [80] str;
     sprintf(str, "This is a number = %d", 314356);
     writefln("%s", str);
 but get error
 Error: function core.stdc.stdio.sprintf (char* s, const(char*) 
 format, ...) is not callable using argument types (char[80], 
 string, int)

 Nor does this work
    char [50] temp = "This is a number";
    string greeting5= temp~" "~314356;
    writefln("%s",greeting5);
import std.conv; string temp = "This is a number"; string greeting5 = temp ~ " " ~ to!string(314356);
 result in error:
 Error: incompatible types for ((cast(const(char)[])temp ~ " ") 
 ~ (314356)): 'char[]' and 'int'


 Any ideas or hints?
 /anders
Nov 29 2016
parent reply Anders S <anders xore.se> writes:
Thanks guys for a really quick answer !!
OK, a little bit awkward to use but getting there
posting a new question about char * to struct ;)

Thanks
/anders
Nov 29 2016
parent Andrea Fontana <nospam example.com> writes:
On Tuesday, 29 November 2016 at 15:01:37 UTC, Anders S wrote:
 Thanks guys for a really quick answer !!
 OK, a little bit awkward to use but getting there
 posting a new question about char * to struct ;)

 Thanks
 /anders
Also: import std.conv : text; string temp = "This is a number"; string greeting5 = text(temp, " ", 314356); Andrea
Nov 30 2016
prev sibling parent Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Tuesday, 29 November 2016 at 10:21:24 UTC, Anders S wrote:
 Hi guys,

 just started to get into Dlang, comming from C and C++ I like 
 to use methods like there if possible.

 Now I want to catenate something like this, but don't get it to 
 work
 in standard C i code:
    char str[80];
    sprintf(str, "This is a number = %f", 3.14356);

 Now in Dlang and import core.stdc.string and
 code:
     char [80] str;
     sprintf(str, "This is a number = %d", 314356);
     writefln("%s", str);
sprintf(str.ptr, "This is a number = %d".toStringz,314356);
 but get error
 Error: function core.stdc.stdio.sprintf (char* s, const(char*) 
 format, ...) is not callable using argument types (char[80], 
 string, int)
because in D arrays do not decay to pointers. To get a null terminated string use toStringz
 Nor does this work
    char [50] temp = "This is a number";
    string greeting5= temp~" "~314356;
    writefln("%s",greeting5);
add a to!string 314356.to!string
 result in error:
 Error: incompatible types for ((cast(const(char)[])temp ~ " ") 
 ~ (314356)): 'char[]' and 'int'


 Any ideas or hints?
 /anders
Nov 29 2016