次の方法で共有


String.ToCharArray メソッド (Int32, Int32)

このインスタンスの指定した部分文字列の文字を Unicode 文字配列へコピーします。

Overloads Public Function ToCharArray( _
   ByVal startIndex As Integer, _   ByVal length As Integer _) As Char()
[C#]
public char[] ToCharArray(intstartIndex,intlength);
[C++]
public: __wchar_t ToCharArray(intstartIndex,intlength)  __gc[];
[JScript]
public function ToCharArray(
   startIndex : int,length : int) : Char[];

パラメータ

  • startIndex
    このインスタンス内の部分文字列の開始位置。
  • length
    このインスタンス内の文字列の長さ。

戻り値

このインスタンスの文字位置 startIndex から、length の数の文字を要素とする Unicode 文字配列。

例外

例外の種類 条件
ArgumentOutOfRangeException startIndex または length が 0 未満です。

または

startIndex と length を合計した値が、このインスタンスの長さよりも大きい値です。

解説

length が 0 の場合、 startIndex の値は無視され、文字列全体がこのインスタンスの先頭からコピーされます。このインスタンスが空の文字列である場合、返される配列は空で、長さは 0 になります。

使用例

 
' Sample for String.ToCharArray(Int32, Int32)
Imports System

Class Sample
   
   Public Shared Sub Main()
      Dim str As String = "012wxyz789"
      Dim arr() As Char
      
      arr = str.ToCharArray(3, 4)
      Console.Write("The letters in '{0}' are: '", str)
      Console.Write(arr)
      Console.WriteLine("'")
      Console.WriteLine("Each letter in '{0}' is:", str)
      Dim c As Char
      For Each c In arr
         Console.WriteLine(c)
      Next c
   End Sub 'Main
End Class 'Sample
'
'This example produces the following results:
'The letters in '012wxyz789' are: 'wxyz'
'Each letter in '012wxyz789' is:
'w
'x
'y
'z
'

[C#] 
// Sample for String.ToCharArray(Int32, Int32)
using System;

class Sample {
    public static void Main() {
    string str = "012wxyz789";
    char[] arr;

    arr = str.ToCharArray(3, 4);
    Console.Write("The letters in '{0}' are: '", str);
    Console.Write(arr);
    Console.WriteLine("'");
    Console.WriteLine("Each letter in '{0}' is:", str);
    foreach (char c in arr)
        Console.WriteLine(c);
    }
}
/*
This example produces the following results:
The letters in '012wxyz789' are: 'wxyz'
Each letter in '012wxyz789' is:
w
x
y
z
*/

[C++] 
// Sample for String::ToCharArray(Int32, Int32)
#using <mscorlib.dll>

using namespace System;
using namespace System::Collections;

int main()
{
   String* str = S"012wxyz789";
   Char arr[];

   arr = str->ToCharArray(3, 4);
   Console::Write(S"The letters in '{0}' are: '", str);
   Console::Write(arr);
   Console::WriteLine(S"'");
   Console::WriteLine(S"Each letter in '{0}' is:", str);
   IEnumerator* myEnum = arr->GetEnumerator();
   while (myEnum->MoveNext()) {
      Char c = *__try_cast<Char*>(myEnum->Current);

      Console::WriteLine(c);
   }
}
/*
This example produces the following results:
The letters in '012wxyz789' are: 'wxyz'
Each letter in '012wxyz789' is:
w
x
y
z
*/

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, Common Language Infrastructure (CLI) Standard

参照

String クラス | String メンバ | System 名前空間 | String.ToCharArray オーバーロードの一覧 | Array | Char | Int32