サーチ…


備考

  • Dの文字列は不変です。インプレース編集する場合は、 .dupを使用して可変char配列を作成します。

文字列を反転する

stringalias string = immutable(char)[];として定義されalias string = immutable(char)[]; :それは反転する前に、 dupを使用して可変char配列を作成する必要があります:

import std.stdio;
import std.string;

int main() {

    string x = "Hello world!";
    char[] x_rev = x.dup.reverse;
    
    writeln(x_rev); // !dlrow olleH
    
    return 0;

}

空の文字列または空文字列のテスト

空の文字列

空文字列はヌルではありませんが長さはゼロです:

string emptyString = "";
// an empty string is not null...
assert(emptyString !is null);

// ... but it has zero lenght
assert(emptyString.length == 0);

ヌル文字列

string nullString = null;

null文字列がnullの場合(De Lapalisse)

assert(nullString is null);

しかし、C#とは異なり、null文字列の長さを読み取ってもエラーは発生しません。

assert(nullString.length == 0);
assert(nullString.empty);

空またはnullのテスト


if (emptyOrNullString.length == 0) {
}

// or
if (emptyOrNullString.length) {
}

// or
import std.array;
if (emptyOrNullString.empty) {
}

nullのテスト

if (nullString is null) {
}

参考文献

文字列をubyte []に​​変換し、その逆も同様です

文字列を不変のubyte[]

string s = "unogatto";
immutable(ubyte[]) ustr = cast(immutable(ubyte)[])s;

assert(typeof(ustr).stringof == "immutable(ubyte[])");
assert(ustr.length == 8);
assert(ustr[0] == 0x75); //u
assert(ustr[1] == 0x6e); //n
assert(ustr[2] == 0x6f); //o
assert(ustr[3] == 0x67); //g
assert(ustr[7] == 0x6f); //o

文字列をubyte[]ubyte[]

string s = "unogatto";
ubyte[] mustr = cast(ubyte[])s;

assert(typeof(mustr).stringof == "ubyte[]");

assert(mustr.length == 8);
assert(mustr[0] == 0x75);
assert(mustr[1] == 0x6e);
assert(mustr[2] == 0x6f);
assert(mustr[3] == 0x67);
assert(mustr[7] == 0x6f);

ubyte[]を文字列にubyte[]する

ubyte[] stream = [ 0x75, 0x6e, 0x6f, 0x67];
string us  = cast(string)stream;
assert(us == "unog");

参考文献



Modified text is an extract of the original Stack Overflow Documentation
ライセンスを受けた CC BY-SA 3.0
所属していない Stack Overflow