Condividi tramite


String.CopyTo Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Copies a specified number of characters from a specified position in this instance to a specified position in an array of Unicode characters.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Sub CopyTo ( _
    sourceIndex As Integer, _
    destination As Char(), _
    destinationIndex As Integer, _
    count As Integer _
)
[SecuritySafeCriticalAttribute]
public void CopyTo(
    int sourceIndex,
    char[] destination,
    int destinationIndex,
    int count
)

Parameters

  • sourceIndex
    Type: System.Int32
    The index of the first character in this instance to copy.
  • destination
    Type: array<System.Char[]
    An array of Unicode characters to which characters in this instance are copied.
  • destinationIndex
    Type: System.Int32
    An index in destination at which the copy operation begins.
  • count
    Type: System.Int32
    The number of characters in this instance to copy to destination.

Exceptions

Exception Condition
ArgumentNullException

destination is nulla null reference (Nothing in Visual Basic).

ArgumentOutOfRangeException

sourceIndex, destinationIndex, or count is negative

-or-

count is greater than the length of the substring from startIndex to the end of this instance

-or-

count is greater than the length of the subarray from destinationIndex to the end of destination

Remarks

count characters are copied from the sourceIndex position of this instance to the destinationIndex position of destination.

sourceIndex and destinationIndex are zero-based.

Examples

The following code example demonstrates the CopyTo method.

Public Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      ' Embed an array of characters in a string
      Dim strSource As String = "changed"
      Dim destination As Char() = {"T"c, "h"c, "e"c, " "c, "i"c, "n"c, "i"c, _
                  "t"c, "i"c, "a"c, "l"c, " "c, "a"c, "r"c, "r"c, "a"c, "y"c}

      ' Print the char array
      outputBlock.Text &= destination & vbCrLf

      ' Embed the source string in the destination string
      strSource.CopyTo(0, destination, 4, strSource.Length)

      ' Print the resulting array
      outputBlock.Text &= destination & vbCrLf

      strSource = "A different string"

      ' Embed only a section of the source string in the destination
      strSource.CopyTo(2, destination, 3, 9)

      ' Print the resulting array
      outputBlock.Text &= destination & vbCrLf
   End Sub
End Class
' The example displays the following output:
'       The initial array
'       The changed array
'       Thedifferentarray
using System;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {

      // Embed an array of characters in a string
      string strSource = "changed";
      char[] destination = { 'T', 'h', 'e', ' ', 'i', 'n', 'i', 't', 'i', 'a', 'l', ' ',
                'a', 'r', 'r', 'a', 'y' };

      // Print the char array
      outputBlock.Text += destination + "\n";

      // Embed the source string in the destination string
      strSource.CopyTo(0, destination, 4, strSource.Length);

      // Print the resulting array
      outputBlock.Text += destination + "\n";

      strSource = "A different string";

      // Embed only a section of the source string in the destination
      strSource.CopyTo(2, destination, 3, 9);

      // Print the resulting array
      outputBlock.Text += destination + "\n";
   }
}
// The example displays the following output:
//       The initial array
//       The changed array
//       Thedifferentarray

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.