如何将ac字符串转换为广告字符串?

def*_*ode 11 d

这很简单,我很尴尬地问,但是如何在D2中将交流字符串转换为广告字符串?

我有两个用例.

string convert( const(char)* c_str );
string convert( const(char)* c_str, size_t length );
Run Code Online (Sandbox Code Playgroud)

Vla*_*eev 16

  1. 使用std.string.toString(char*)(D1/Phobos)或std.conv.to!(string)(D2):

    // D1
    import std.string; 
    ... 
    string s = toString(c_str);
    
    // D2
    import std.conv;
    ...
    string s = to!(string)(c_str);
    
    Run Code Online (Sandbox Code Playgroud)
  2. 切指针:

    string s = c_str[0..len];
    
    Run Code Online (Sandbox Code Playgroud)

    (您不能使用"length",因为它与切片语法有特殊含义).

两者都将在C字符串上返回一个切片(因此,引用而不是副本).使用.dup属性创建副本.

请注意,D字符串被认为是UTF-8编码.如果你的字符串是另一种编码,你需要转换它(例如使用std.windows.charset中的函数).