खोज…


टिप्पणियों

  • डी में स्ट्रिंग्स अपरिवर्तनीय हैं; यदि आप इन-प्लेस को संपादित करना चाहते हैं, तो एक परिवर्तनशील char सरणी बनाने के लिए .dup का उपयोग करें।

एक स्ट्रिंग उलट

string को alias string = immutable(char)[]; रूप में परिभाषित किया गया है alias string = immutable(char)[]; : इससे पहले कि इसे उलटा किया जा सके, एक परिवर्तनशील चार सरणी बनाने के लिए dup का उपयोग करने की आवश्यकता है:

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;

अशक्त स्ट्रिंग अशक्त है (डी लापालिस)

assert(nullString is null);

लेकिन, C # के विपरीत, एक अशक्त स्ट्रिंग की लंबाई पढ़ें त्रुटि उत्पन्न नहीं करता है:

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

खाली या अशक्त के लिए परीक्षण करें


if (emptyOrNullString.length == 0) {
}

// or
if (emptyOrNullString.length) {
}

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

अशक्त के लिए परीक्षण

if (nullString is null) {
}

संदर्भ

Ubyte [] और इसके विपरीत में स्ट्रिंग बदलें

अपरिवर्तनीय 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[] 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