D 程式設計 - 字串



D 提供以下兩種型別的字串表示形式:

  • 字元陣列
  • 核心語言字串

字元陣列

我們可以用以下兩種形式之一表示字元陣列。第一種形式直接提供大小,第二種形式使用 dup 方法,該方法建立字串“Good morning”的可寫副本。

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
廣告