On Tuesday, 22 December 2020 at 21:10:59 UTC, Godnyx wrote:
Is there a way? If not then how std.stdio does it?
I assume you’re asking this because you don’t have access to
std.stdio (such as using betterC).
The way to do it is to use the %.*s specifier in printf.
For example:
void print_string(string text){
printf(“%.*s\n”, cast(int)text.length, text.ptr);
}
The ‘.N' in front of the ’s’ says to not print more than N
characters from the char*. using a ‘*’ says that the actual
number of characters will be passed as an argument to printf
instead of a hardcoded number. This is specified to be an int, so
we have to cast the length of the string to int when calling
printf. Finally, we need to pass the pointer to the actual
character data, thus the text.ptr.