Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Previous Versions
.NET Framework 2.0
System
String Class
String Methods
 CopyTo Method

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
.NET Framework Class Library
String.CopyTo Method

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)

Visual Basic (Declaration)
Public Sub CopyTo ( _
    sourceIndex As Integer, _
    destination As Char(), _
    destinationIndex As Integer, _
    count As Integer _
)
Visual Basic (Usage)
Dim instance As String
Dim sourceIndex As Integer
Dim destination As Char()
Dim destinationIndex As Integer
Dim count As Integer

instance.CopyTo(sourceIndex, destination, destinationIndex, count)
C#
public void CopyTo (
    int sourceIndex,
    char[] destination,
    int destinationIndex,
    int count
)
C++
public:
void CopyTo (
    int sourceIndex, 
    array<wchar_t>^ destination, 
    int destinationIndex, 
    int count
)
J#
public void CopyTo (
    int sourceIndex, 
    char[] destination, 
    int destinationIndex, 
    int count
)
JScript
public function CopyTo (
    sourceIndex : int, 
    destination : char[], 
    destinationIndex : int, 
    count : int
)

Parameters

sourceIndex

A character position in this instance.

destination

An array of Unicode characters.

destinationIndex

An array element in destination.

count

The number of characters in this instance to copy to destination.

Exception typeCondition

ArgumentNullException

destination is a 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

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

sourceIndex and destinationIndex are zero-based.

The following code example demonstrates the CopyTo method.

Visual Basic
Imports System

Public Class CopyToTest

    Public Shared Sub Main()

        ' 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
        Console.WriteLine(destination)

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

        ' Print the resulting array
        Console.WriteLine(destination)

        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
        Console.WriteLine(destination)
    End Sub 'Main
End Class 'CopyToTest
C#
using System;

public class CopyToTest {
    public static void Main() {

        // 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
        Console.WriteLine( destination );

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

        // Print the resulting array
        Console.WriteLine( destination );

        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
        Console.WriteLine( destination );
    }
}
C++
using namespace System;
int main()
{
   
   // Embed an array of characters in a string
   String^ strSource = "changed";
   array<Char>^destination = {'T','h','e',' ','i','n','i','t','i','a','l',' ','a','r','r','a','y'};
   
   // Print the char array
   Console::WriteLine( destination );
   
   // Embed the source string in the destination string
   strSource->CopyTo( 0, destination, 4, strSource->Length );
   
   // Print the resulting array
   Console::WriteLine( destination );
   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
   Console::WriteLine( destination );
}

J#
import System.*;

public class CopyToTest
{
    public static void main(String[] args)
    {
        // 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
        Console.WriteLine(destination);
        // Embed the source string in the destination string
        strSource.CopyTo(0, destination, 4, strSource.get_Length());
        // Print the resulting array
        Console.WriteLine(destination);

        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
        Console.WriteLine(destination);
    } //main
} //CopyToTest
JScript
import System;

public class CopyToTest {
    public static function Main() : void {

        // Embed an array of characters in a string
        var strSource : String = "changed";
        var destination : char[] = "The initial array".ToCharArray();

        // Print the char array
        Console.WriteLine( destination );

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

        // Print the resulting array
        Console.WriteLine( destination );

        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
        Console.WriteLine( destination );
    }
}
CopyToTest.Main();

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

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker