我有一个标准的字符指针,我试图将其转换为字符串。
// string to char*
char *x = cast(char*)("Hello World\0");
// char* to string?
string x = cast(string)x;
string x = cast(immutable(char)[])x;
错误!
有什么想法如何将 char* 转换为 D 中的字符串吗?
最佳答案
使用std.conv.to
将char*
转换为string
。使用 std.string.toStringZ
走另一条路。
import std.string;
import std.stdio;
import std.conv;
void main()
{
immutable(char)* x = "Hello World".toStringz();
auto s = to!string(x);
writeln(s);
}
关于string - 如何将 D 中的 char* 转换为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8876177/