D Language
stringhe
Ricerca…
Osservazioni
- Le stringhe in D sono immutabili; usa
.dup
per creare un array dichar
mutabili se vuoi modificare sul posto.
Inversione di una stringa
string
è definita come alias string = immutable(char)[];
: quindi è necessario usare dup
per creare un char array mutabile, prima che possa essere invertito:
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;
}
Prova per una stringa vuota o nulla
Stringa vuota
La stringa vuota non è nulla ma ha lunghezza zero:
string emptyString = "";
// an empty string is not null...
assert(emptyString !is null);
// ... but it has zero lenght
assert(emptyString.length == 0);
Stringa nulla
string nullString = null;
una stringa nulla è nulla (De Lapalisse)
assert(nullString is null);
ma, a differenza di C #, leggere la lunghezza di una stringa nulla non genera errore:
assert(nullString.length == 0);
assert(nullString.empty);
Prova per vuoto o nullo
if (emptyOrNullString.length == 0) {
}
// or
if (emptyOrNullString.length) {
}
// or
import std.array;
if (emptyOrNullString.empty) {
}
Prova per null
if (nullString is null) {
}
Riferimenti
Converti stringa in ubyte [] e viceversa
Stringa a ubyte[]
immutabile 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
Stringa per 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[]
su stringa
ubyte[] stream = [ 0x75, 0x6e, 0x6f, 0x67];
string us = cast(string)stream;
assert(us == "unog");
Riferimenti
Modified text is an extract of the original Stack Overflow Documentation
Autorizzato sotto CC BY-SA 3.0
Non affiliato con Stack Overflow