.NET で新しい文字列を作成する

.NET は、単純な割り当てを使用した文字列の作成をサポートしています。また、多数の異なるパラメーターを使用した文字列を作成できるよう、クラス コンストラクターをオーバーロードできます。 .NET には、System.String クラスにいくつかの文字列、文字列の配列、またはオブジェクトを組み合わせて新しい文字列オブジェクトを作成できるメソッドもいくつかあります。

割り当てを使用した文字列の作成

新しい String オブジェクトを作成する最も簡単な方法としては、単に文字列リテラルを String オブジェクトに割り当てます。

クラス コンストラクターを使用した文字列の作成

String クラス コンストラクターのオーバーロードを使用すると、文字配列から文字列を作成できます。 また、指定した回数だけ特定の文字を複製することで、新しい文字列を作成することもできます。

文字列を返すメソッド

次の表は、新しい文字列オブジェクトを返すいくつかの便利なメソッドを示しています。

メソッド名 使用
String.Format 入力オブジェクトのセットから、書式設定された文字列をビルドします。
String.Concat 2 つ以上の文字列から文字列をビルドします。
String.Join 文字列の配列を組み合わせることで、新しい文字列をビルドします。
String.Insert 既存の文字列の指定のインデックスに文字列を挿入することで、新しい文字列をビルドします。
String.CopyTo 文字配列内の指定の位置に、文字列内の指定の文字をコピーします。

形式

String.Format メソッドを使用すると、書式設定された文字列を作成し、複数のオブジェクトを表す文字列を連結できます。 このメソッドは、渡されたすべてのオブジェクトを文字列に自動的に変換します。 たとえば、アプリケーションでユーザーに対して Int32 値と DateTime 値を表示する必要がある場合、Format メソッドを使用して、これらの値を表す文字列を簡単に作成できます。 このメソッドで使用される書式設定規則については、複合書式指定に関するセクションを参照してください。

次の例では、Format メソッドを使用して、整数型の変数を使用する文字列を作成します。

int numberOfFleas = 12;
string miscInfo = String.Format("Your dog has {0} fleas. " +
                                "It is time to get a flea collar. " +
                                "The current universal date is: {1:u}.",
                                numberOfFleas, DateTime.Now);
Console.WriteLine(miscInfo);
// The example displays the following output:
//       Your dog has 12 fleas. It is time to get a flea collar.
//       The current universal date is: 2008-03-28 13:31:40Z.
Dim numberOfFleas As Integer = 12
Dim miscInfo As String = String.Format("Your dog has {0} fleas. " & _
                                       "It is time to get a flea collar. " & _
                                       "The current universal date is: {1:u}.", _
                                       numberOfFleas, Date.Now)
Console.WriteLine(miscInfo)
' The example displays the following output:
'       Your dog has 12 fleas. It is time to get a flea collar. 
'       The current universal date is: 2008-03-28 13:31:40Z.

この例では、DateTime.Now は、現在のスレッドに関連付けられているカルチャで指定された方法で現在の日時を表示します。

Concat

String.Concat メソッドを使用すると、2 つ以上の既存のオブジェクトから新しい文字列オブジェクトを簡単に作成できます。 言語に依存せずに文字列を連結する方法を提供します。 このメソッドは、System.Object から派生したすべてのクラスを受け入れます。 次の例では、2 つの既存の文字列オブジェクトおよび区切り文字から文字列を作成します。

string helloString1 = "Hello";
string helloString2 = "World!";
Console.WriteLine(String.Concat(helloString1, ' ', helloString2));
// The example displays the following output:
//      Hello World!
Dim helloString1 As String = "Hello"
Dim helloString2 As String = "World!"
Console.WriteLine(String.Concat(helloString1, " "c, helloString2))
' The example displays the following output:
'      Hello World!

Join

String.Join メソッドは、文字列の配列および区切り文字列から新しい文字列を作成します。 このメソッドは、複数の文字列を連結して、たとえばコンマで区切られたリストを作成する場合に便利です。

次の例では、スペースを使用して文字列の配列をバインドします。

string[] words = {"Hello", "and", "welcome", "to", "my" , "world!"};
Console.WriteLine(String.Join(" ", words));
// The example displays the following output:
//      Hello and welcome to my world!
Dim words() As String = {"Hello", "and", "welcome", "to", "my", "world!"}
Console.WriteLine(String.Join(" ", words))
' The example displays the following output:
'      Hello and welcome to my world!

挿入

String.Insert メソッドは、別の文字列内の指定の位置に文字列を挿入することで、新しい文字列を作成します。 このメソッドは、0 から始まるインデックスを使用します。 次の例では、MyString の 5 番目のインデックス位置に文字列を挿入し、この値を持つ新しい文字列を作成します。

string sentence = "Once a time.";
 Console.WriteLine(sentence.Insert(4, " upon"));
 // The example displays the following output:
 //      Once upon a time.
Dim sentence As String = "Once a time."
Console.WriteLine(sentence.Insert(4, " upon"))
' The example displays the following output:
'      Once upon a time.

CopyTo

String.CopyTo メソッドは、文字配列に文字列の部分をコピーします。 文字列の開始インデックスと、コピーする文字数の両方を指定できます。 このメソッドは、ソース インデックス、文字配列、コピー先のインデックス、およびコピーする文字数を受け取ります。 すべてのインデックスは 0 から始まります。

次の例では、CopyTo メソッドを使用して、文字列オブジェクトから文字配列の最初のインデックス位置に、単語 "Hello" の文字をコピーします。

string greeting = "Hello World!";
char[] charArray = {'W','h','e','r','e'};
Console.WriteLine("The original character array: {0}", new string(charArray));
greeting.CopyTo(0, charArray,0 ,5);
Console.WriteLine("The new character array: {0}", new string(charArray));
// The example displays the following output:
//       The original character array: Where
//       The new character array: Hello
Dim greeting As String = "Hello World!"
Dim charArray() As Char = {"W"c, "h"c, "e"c, "r"c, "e"c}
Console.WriteLine("The original character array: {0}", New String(charArray))
greeting.CopyTo(0, charArray, 0, 5)
Console.WriteLine("The new character array: {0}", New String(charArray))
' The example displays the following output:
'       The original character array: Where
'       The new character array: Hello

関連項目