次の方法で共有


String コンストラクタ (Char , Int32, Int32)

String クラスの新しいインスタンスを初期化し、Unicode 文字の配列、配列内の開始文字位置、および長さにより示される値に設定します。

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)

構文

'宣言
Public Sub New ( _
    value As Char(), _
    startIndex As Integer, _
    length As Integer _
)
'使用
Dim value As Char()
Dim startIndex As Integer
Dim length As Integer

Dim instance As New String(value, startIndex, length)
public String (
    char[] value,
    int startIndex,
    int length
)
public:
String (
    array<wchar_t>^ value, 
    int startIndex, 
    int length
)
public String (
    char[] value, 
    int startIndex, 
    int length
)
public function String (
    value : char[], 
    startIndex : int, 
    length : int
)

パラメータ

  • value
    Unicode 文字の配列。
  • startIndex
    value 内の開始位置。
  • length
    使用する value 内の文字数。

例外

例外の種類 条件

ArgumentNullException

value が null 参照 (Visual Basic では Nothing) です。

ArgumentOutOfRangeException

startIndex または length が 0 未満です。

または

startIndex と length の合計値が、value 内の要素数より大きい値です。

解説

length が 0 の場合は、Empty インスタンスが初期化されます。

このコンストラクタは value の startIndex から (startIndex + length - 1) までの Unicode 文字をコピーします。

パフォーマンスに関する考慮事項

テキスト ストリームを解析またはデコードするアプリケーションでは、一連の文字を文字列に変換するために、String コンストラクタまたは StringBuilder.Append メソッドが頻繁に使用されます。まったく同じ一連の文字が繰り返し出現する場合、その都度、同じ値を使って新しい文字列を作成していると、文字列を一度だけ作成して再利用した場合に比べ、メモリの消費が激しくなります。

まったく同じ一連の文字が出現することはわかっていても、具体的な中身を予測できない場合は、String コンストラクタで新しい String オブジェクトを作成するのではなく、ルックアップ テーブルの使用を検討してください。たとえば、XML のタグおよび属性を含むファイルから文字のストリームを読み込んで解析するアプリケーションを考えてみます。このストリームを解析すると、特定のトークン (記号的意味合いを持つ一連の文字) が繰り返し出現します。XML ストリームでは、"0"、"1"、"true"、"false" などの文字列に相当するトークンが頻繁に現れる傾向があります。

出現する各トークンを新しい文字列に変換する代わりに、NameTable オブジェクトを作成して、よく使用される文字列を保持できます。NameTable オブジェクトは、格納された文字列を取得するときに一時メモリを割り当てないので、パフォーマンスが向上します。

アプリケーションはトークンを見つけると、NameTable.Get メソッドを使用してそのトークンをテーブルから取得しようとします。トークンが存在する場合、メソッドは対応する文字列を返します。トークンが存在しない場合は、NameTable.Add メソッドを使用して、トークンをテーブルに挿入します。トークンを挿入すると、メソッドは対応する文字列を返します。いずれの場合も、アプリケーションに適した文字列が返されます。

使用例

このコンストラクタを使用して String クラスのインスタンスを作成する方法を次の簡単なコード例で示します。

// Create a Unicode String with 5 Greek Alpha characters
String szGreekAlpha = new String('\u0319',5);
// Create a Unicode String with a Greek Omega character
String szGreekOmega = new String(new char [] {'\u03A9','\u03A9','\u03A9'},2,1);

String szGreekLetters = String.Concat(szGreekOmega, szGreekAlpha, szGreekOmega.Clone());

// Examine the result
Console.WriteLine(szGreekLetters);

// The first index of Alpha
int ialpha = szGreekLetters.IndexOf('\u0319');
// The last index of Omega
int iomega = szGreekLetters.LastIndexOf('\u03A9');

Console.WriteLine("The Greek letter Alpha first appears at index " + ialpha +
    " and Omega last appears at index " + iomega + " in this String.");
// Create a Unicode String with 5 Greek Alpha characters
String^ szGreekAlpha = gcnew String( L'\x0319',5 );

// Create a Unicode String with a Greek Omega character
wchar_t charArray5[3] = {L'\x03A9',L'\x03A9',L'\x03A9'};
String^ szGreekOmega = gcnew String( charArray5,2,1 );
String^ szGreekLetters = String::Concat( szGreekOmega, szGreekAlpha, szGreekOmega->Clone() );

// Examine the result
Console::WriteLine( szGreekLetters );

// The first index of Alpha
int ialpha = szGreekLetters->IndexOf( L'\x0319' );

// The last index of Omega
int iomega = szGreekLetters->LastIndexOf( L'\x03A9' );
Console::WriteLine( String::Concat(  "The Greek letter Alpha first appears at index ", Convert::ToString( ialpha ) ) );
Console::WriteLine( String::Concat(  " and Omega last appears at index ", Convert::ToString( iomega ),  " in this String." ) );

プラットフォーム

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。

バージョン情報

.NET Framework

サポート対象 : 2.0、1.1、1.0

.NET Compact Framework

サポート対象 : 2.0、1.0

参照

関連項目

String クラス
String メンバ
System 名前空間
Char 構造体
Int32 構造体