.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!
参加する
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.
コピー先へ
String.CopyTo
メソッドは、文字列の一部を文字の配列にコピーします。 文字列の先頭インデックスとコピーする文字数の両方を指定できます。 このメソッドは、コピー元のインデックス、文字の配列、コピー先のインデックス、およびコピーする文字数を受け取ります。 すべてのインデックスは 0 から始まります。
次の例では、 CopyTo
メソッドを使用して、文字列オブジェクトから文字配列の最初のインデックス位置に単語 "Hello" の文字をコピーします。
string greeting = "Hello World!";
char[] charArray = {'W','h','e','r','e'};
Console.WriteLine($"The original character array: {new string(charArray)}");
greeting.CopyTo(0, charArray,0 ,5);
Console.WriteLine($"The new character array: {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
こちらも参照ください
.NET