Edit

Share via


String.CopyTo Method

Definition

Overloads

CopyTo(Span<Char>)

Copies the contents of this string into the destination span.

CopyTo(Int32, Char[], Int32, Int32)

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

CopyTo(Span<Char>)

Source:
String.cs
Source:
String.cs
Source:
String.cs

Copies the contents of this string into the destination span.

C#
public void CopyTo(Span<char> destination);

Parameters

destination
Span<Char>

The span into which to copy this string's contents.

Exceptions

The destination span is shorter than the source string.

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9

CopyTo(Int32, Char[], Int32, Int32)

Source:
String.cs
Source:
String.cs
Source:
String.cs

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

C#
public void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count);

Parameters

sourceIndex
Int32

The index of the first character in this instance to copy.

destination
Char[]

An array of Unicode characters to which characters in this instance are copied.

destinationIndex
Int32

The index in destination at which the copy operation begins.

count
Int32

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

Exceptions

destination is null.

sourceIndex, destinationIndex, or count is negative

-or-

sourceIndex does not identify a position in the current instance.

-or-

destinationIndex does not identify a valid index in the destination array.

-or-

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

-or-

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

Examples

The following example demonstrates the CopyTo method.

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 );
    }
}
// The example displays the following output:
//       The initial array
//       The changed array
//       Thedifferentarray

Remarks

This method copies count characters from the sourceIndex position of this instance to the destinationIndex position of destination character array. This method does not resize the destination character array; it must have a sufficient number of elements to accommodate the copied characters or the method throws an ArgumentOutOfRangeException.

sourceIndex and destinationIndex are zero-based.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0