D-programming-strings

提供:Dev Guides
移動先:案内検索

Dプログラミング-文字列

Dは、次の2種類の文字列表現を提供します-

  • 文字配列
  • コア言語文字列

文字配列

以下に示すように、2つの形式のいずれかで文字配列を表すことができます。 最初のフォームはサイズを直接提供し、2番目のフォームは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

ひもの長さ

バイト単位の文字列の長さは、長さ関数を使用して取得できます。

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