如何将字符*转换为D中的字符串? [英] How to cast a char* to string in D?
本文介绍了如何将字符*转换为D中的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个标准的字符指针,它试图转换为字符串。
I have a standard char pointer which im trying to cast to a string.
// string to char*
char *x = cast(char*)("Hello World\0");
// char* to string?
string x = cast(string)x;
string x = cast(immutable(char)[])x;
错误!
将一个字符*转换为D中的字符串
Any ideas how to cast a char* to a string in D?
推荐答案
使用 std.conv.to < c $ c>将
char *
转换为 string
。
import std.string;
import std.stdio;
import std.conv;
void main()
{
immutable(char)* x = "Hello World".toStringz();
auto s = to!string(x);
writeln(s);
}
这篇关于如何将字符*转换为D中的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文