目录

D编程 - 字符串(Strings)

D提供以下两种类型的字符串表示 -

  • 字符数组
  • 核心语言字符串

字符数组

我们可以用两种形式之一表示字符数组,如下所示。 第一个表单直接提供大小,第二个表单使用dup方法创建字符串“早上好”的可写副本。

char[9]  greeting1 = "Hello all"; 
char[] greeting2 = "Good morning".dup; 

例子 (Example)

这是一个使用上述简单字符数组形式的简单示例。

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";

例子 (Example)

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 

字符串连接 (String Concatenation)

D编程中的字符串连接使用波浪号(〜)符号。

例子 (Example)

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 

字符串长度

借助长度函数可以检索字符串的长度(以字节为单位)。

例子 (Example)

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编程中字符串比较非常简单。 您可以使用==,运算符进行字符串比较。

例子 (Example)

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'.

替换字符串

我们可以使用字符串[]替换字符串。

例子 (Example)

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的字符串中子字符串位置的索引方法。

例子 (Example)

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

处理案件

以下示例显示了用于更改案例的方法。

例子 (Example)

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

限制字符

字符串中的Restring字符显示在以下示例中。

例子 (Example)

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
↑回到顶部↑
WIKI教程 @2018