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