D语言提供了以下两种类型的字符串表示:
字符数组。
核心语言字符串。
我们可以表示两种形式,如下所示的一个字符数组。第一种形式直接提供的大小和第二种形式使用它创建字符串的可写副本“Good morning”的dup方法。
char[9] greeting1= "Hello all"; char[] greeting2 = "Good morning".dup;
下面是使用上述简单的字符数组的形式一个简单的例子。
import std.stdio; |
void main(string[] args) |
{ |
char[9] greeting1= "Hello all"; |
writefln("%s",greeting1); |
char[] greeting2 = "Good morning".dup; |
writefln("%s",greeting2); |
} |
Hello all |
Good morning |
字符串是内置在D核心语言,这些字符串是互操作与上面显示的字符数组。下面的例子显示了一个简单的字符串表示形式。
string greeting1= "Hello all"; |
import std.stdio; |
void main(string[] args) |
{ |
string greeting1= "Hello all"; |
writefln("%s",greeting1); |
char[] greeting2 = "Good morning".dup; |
writefln("%s",greeting2); |
string greeting3= greeting1; |
writefln("%s",greeting3); |
} |
Hello all Good morning Hello all
D编程语言的字符串连接使用符号(〜)符号。一个简单的字符串连接示例如下所示。
import std.stdio; |
void main(string[] args) |
{ |
string greeting1= "Good"; |
char[] greeting2 = "morning".dup; |
char[] greeting3= greeting1~" "~greeting2; |
writefln("%s",greeting3); |
string greeting4= "morning"; |
string greeting5= greeting1~" "~greeting4; |
writefln("%s",greeting5); |
} |
Good morning Good morning
字符串的字节长度可以length功能的帮助下检索。一个简单的例子如下所示。
import std.stdio; |
void main(string[] args) |
{ |
string greeting1= "Good"; |
writefln("Length of string greeting1 is %d",greeting1.length); |
char[] greeting2 = "morning".dup; |
writefln("Length of string greeting2 is %d",greeting2.length); |
} |
Length of string greeting1 is 4 |
Length of string greeting2 is 7 |
字符串比较在D语言编程中相当容易。可以使用==,<和>运算符进行字符串比较。一个简单的例子如下所示。
import std.stdio; |
void main() |
{ |
string s1 = "Hello"; |
string s2 = "World"; |
string s3 = "World"; |
if (s2 == s3) |
{ |
writeln("s2: ",s2," and S3: ",s3, " are the same!"); |
} |
if (s1 < s2) |
{ |
writeln("'", s1, "' comes before '", s2, "'."); |
} |
else |
{ |
writeln("'", s2, "' comes before '", s1, "'."); |
} |
} |
s2: World and S3: World are the same! |
'Hello' comes before 'World'. |
我们可以使用string[]替换字符串。一个简单的例子如下所示。
import std.stdio; |
import std.string; |
void main() |
{ |
char[] s1 = "hello world ".dup; |
char[] s2 = "sample".dup; |
s1[6..12] = s2[0..6]; |
writeln(s1); |
} |
hello sample |
索引方法在字符串的indexOf包括和lastIndexOf子串的位置,在下面的例子来说明。
import std.stdio; |
import std.string; |
void main() |
{ |
char[] s1 = "hello World ".dup; |
writeln("indexOf of llo in hello is ",std.string.indexOf(s1,"llo")); |
writeln(s1); |
writeln("lastIndexOf of O in hello is" |
,std.string.lastIndexOf(s1,"O",CaseSensitive.no)); |
} |
indexOf of llo in hello is 2 |
hello World |
lastIndexOf of O in hello is 7 |
用于改变大小的方法示于下面的例子。
import std.stdio; |
import std.string; |
void main() |
{ |
char[] s1 = "hello World ".dup; |
writeln("Capitalized string of s1 is ",capitalize(s1)); |
writeln("Uppercase string of s1 is ",toUpper(s1)); |
writeln("Lowercase string of s1 is ",toLower(s1)); |
} |
Capitalized string of s1 is Hello world Uppercase string of s1 is HELLO WORLD Lowercase string of s1 is hello world
在字符串限制字符显示在下面的例子。
import std.stdio; |
import std.string; |
void main() |
{ |
string s = "H123Hello1"; |
string result = munch(s, "0123456789H"); |
writeln("Restrict trailing characters:",result); |
result = squeeze(s, "0123456789H"); |
writeln("Restrict leading characters:",result); |
s = " Hello World "; |
writeln("Stripping leading and trailing whitespace:",strip(s)); |
} |
Restrict trailing characters:H123H |
Restrict leading characters:ello1 |
Stripping leading and trailing whitespace:Hello World |