string - 如何将 D 中的 char* 转换为字符串?

标签 string pointers casting char d

我有一个标准的字符指针,我试图将其转换为字符串。

// 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.tochar*转换为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/

相关文章:

python - 如何在恒定时间内在python中查找列表/元组的大小

c - c中的赋值和比较指针

sql - PostgreSQL:在 timestamp::DATE 上创建索引

java - String.valueOf(char) 和 + 的区别

string - 将 Percent-Encoded URI 字符串中的汉字转换为 UTF-8 中的汉字?

java - 在 Java 中使用函数指针作为简单的命令行解释器

c# - 在 C# 中重铸为基类

casting - CUDA 和 cuBLAS 中的类型转换

xcode - 字符串不可转换为 int (Swift)

java - 创建了多少字符串对象(String.format())?