字符数组
我们可以用以下两种形式来表示字符数组.第一种形式直接提供大小,第二种形式使用dup方法创建字符串" Good morning"。
char[9] greeting1="Hello learnfk"; char[] greeting2="Good morning".dup;
这是使用上述简单字符数组形式的简单示例。
import std.stdio; void main(string[] args) { char[9] greeting1="Hello learnfk"; writefln("%s",greeting1); char[] greeting2="Good morning".dup; writefln("%s",greeting2); }
当上面的代码被编译和执行时,它产生的输出如下:
Hello learnfk Good morning
字符串
这些字符串可与上面显示的字符数组互操作,以下示例显示了一个简单的字符串表示形式。
string greeting1="Hello learnfk";
字符串示例
import std.stdio; void main(string[] args) { string greeting1="Hello learnfk"; writefln("%s",greeting1); char[] greeting2="Good morning".dup; writefln("%s",greeting2); string greeting3=greeting1; writefln("%s",greeting3); }
当上面的代码被编译和执行时,它产生的输出如下:
Hello learnfk Good morning Hello all
字符串拼接
D编程中的字符串连接使用tilde(~)符号。
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函数
可以使用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="Learnfk"; string s3="Learnfk"; 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: Learnfk and S3: Learnfk are the same! 'Hello' comes before 'Learnfk'.
字符串替换
我们可以使用string [] replace字符串。
import std.stdio; import std.string; void main() { char[] s1="hello Learnfk ".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 Learnfk ".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 Learnfk lastIndexOf of O in hello is 7
字符处理
以下示例显示了用于更改字符示例的方法。
import std.stdio; import std.string; void main() { char[] s1="hello Learnfk ".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 Learnfk Uppercase string of s1 is HELLO LEARNFK Lowercase string of s1 is hello Learnfk
限制字符
下示例显示了字符串中的重串字符。
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 Learnfk "; writeln("Stripping leading and trailing whitespace:",strip(s)); }
编译并执行上述代码后,将产生以下输出-
Restrict trailing characters:H123H Restrict leading characters:ello1 Stripping leading and trailing whitespace:Hello Learnfk
参考链接
https://www.learnfk.com/d-programming/d-programming-strings.html
标签:std,教程,string,s1,无涯,greeting1,import,Learnfk,Strings From: https://blog.51cto.com/u_14033984/8452684