新しい文字列の作成
.NET Framework では、簡単な代入を使用して文字列を作成できます。また、コンストラクターをオーバーロードして、多数のパラメーターを使用した文字列の作成をサポートすることもできます。 .NET Framework にも、文字列、文字列配列、またはオブジェクトを結合して新しい文字列オブジェクトを作成するメソッドが System.String クラスに用意されています。
代入を使用した文字列の作成
新しい String オブジェクトを作成する最も簡単な方法は、単純にリテラル文字列を String オブジェクトに割り当てることです。
クラス コンストラクターを使用した文字列の作成
String クラス コンストラクターのオーバーロードを使用して、文字の配列から文字列を作成できます。 また、特定の文字を指定された数だけ複製して、新しい文字列を作成することもできます。
文字列を返すメソッド
新しい文字列オブジェクトを返す、便利なメソッドの一覧を次の表に示します。
メソッド名 |
使用方法 |
---|---|
一連の入力オブジェクトから、書式設定された文字列を作成します。 |
|
2 つ以上の文字列を連結して文字列を作成します。 |
|
文字列の配列を結合して新しい文字列を作成します。 |
|
既存の文字列内の指定したインデックス位置に文字列を挿入して、新しい文字列を作成します。 |
|
文字の配列内の指定した位置に、文字列内の指定した文字をコピーします。 |
Format
String.Format メソッドを使用すると、書式指定された文字列を作成し、複数のオブジェクトを表す文字列を連結できます。 このメソッドは、渡された任意のオブジェクトを文字列に自動変換します。 たとえば、アプリケーションでユーザーに対して Int32 値と DateTime 値を表示する必要がある場合、これらの値を表す文字列を Format メソッドを使用して簡単に作成できます。 このメソッドで使用される書式指定規則の詳細については、複合書式指定に関するセクションを参照してください。
Format メソッドを使用して、整数変数を使用する文字列を作成する例を次に示します。
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.
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.
この例では、DateTime.Now は現在のスレッドに関連付けられているカルチャで指定される形式で現在の日時を表示します。
Concat
String.Concat メソッドを使用すると、2 つ以上の既存のオブジェクトを連結して新しい文字列オブジェクトを簡単に作成できます。 このメソッドは、言語に依存することなく文字列を連結します。 このメソッドは、System.Object クラスから派生したすべてのクラスを受け入れます。 既存の 2 つの文字列オブジェクトおよび区切り文字から文字列を作成する例を次に示します。
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 helloString1 = "Hello";
string helloString2 = "World!";
Console.WriteLine(String.Concat(helloString1, ' ', helloString2));
// The example displays the following output:
// Hello World!
Join
String.Join メソッドは、文字列の配列と区切り文字から新しい文字列を作成します。 このメソッドは、複数の文字列をコンマなどで区切って連結したリストを作成する場合に便利です。
文字列の配列を空白で区切って連結する例を次に示します。
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[] words = {"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 番目のインデックス位置に挿入し、挿入した値を含む新しい文字列を作成する例を次に示します。
Dim sentence As String = "Once a time."
Console.WriteLine(sentence.Insert(4, " upon"))
' The example displays the following output:
' Once upon a time.
string sentence = "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" を構成する文字を文字配列の 1 番目のインデックス位置にコピーする例を次に示します。
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
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