String.CopyTo Метод

Определение

Перегрузки

Имя Описание
CopyTo(Span<Char>)

Копирует содержимое этой строки в целевой диапазон.

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

Копирует указанное число символов из указанной позиции в этом экземпляре в указанное положение в массиве символов Юникода.

CopyTo(Span<Char>)

Исходный код:
String.cs
Исходный код:
String.cs
Исходный код:
String.cs
Исходный код:
String.cs
Исходный код:
String.cs

Копирует содержимое этой строки в целевой диапазон.

public:
 void CopyTo(Span<char> destination);
public void CopyTo(Span<char> destination);
member this.CopyTo : Span<char> -> unit
Public Sub CopyTo (destination As Span(Of Char))

Параметры

destination
Span<Char>

Диапазон, в который нужно скопировать содержимое этой строки.

Исключения

Диапазон назначения короче исходной строки.

Применяется к

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

Исходный код:
String.cs
Исходный код:
String.cs
Исходный код:
String.cs
Исходный код:
String.cs
Исходный код:
String.cs

Копирует указанное число символов из указанной позиции в этом экземпляре в указанное положение в массиве символов Юникода.

public:
 void CopyTo(int sourceIndex, cli::array <char> ^ destination, int destinationIndex, int count);
public void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count);
member this.CopyTo : int * char[] * int * int -> unit
Public Sub CopyTo (sourceIndex As Integer, destination As Char(), destinationIndex As Integer, count As Integer)

Параметры

sourceIndex
Int32

Индекс первого символа в этом экземпляре для копирования.

destination
Char[]

Массив символов Юникода, в который копируются символы в этом экземпляре.

destinationIndex
Int32

Индекс, с destination которого начинается операция копирования.

count
Int32

Количество символов в этом экземпляре для копирования destination.

Исключения

destination равно null.

sourceIndex, или destinationIndexcount отрицательный

–или–

sourceIndex не определяет позицию в текущем экземпляре.

–или–

destinationIndex не определяет допустимый индекс в массиве destination .

–или–

count больше длины подстроки от sourceIndex конца этого экземпляра

–или–

count больше длины субаррей от destinationIndex конца массива destination .

Примеры

В следующем примере демонстрируется CopyTo метод.

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
// Embed an array of characters in a string
let strSource = "changed"
let destination = 
    [| 'T'; 'h'; 'e'; ' '; 'i'; 'n'; 'i'; 't'; 'i'; 'a'; 'l'; ' ';
       'a'; 'r'; 'r'; 'a'; 'y' |]

// Print the char array
printfn $"{destination}"

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

// Print the resulting array
printfn $"{destination}"

let strSource2 = "A different string"

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

// Print the resulting array
printfn $"{destination}"
// The example displays the following output:
//       The initial array
//       The changed array
//       Thedifferentarray
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 
End Class 
' The example displays the following output:
'       The initial array
'       The changed array
'       Thedifferentarray

Комментарии

Этот метод копирует count символы из sourceIndex позиции этого экземпляра в destinationIndex положение массива символов destination . Этот метод не изменяет destination размер массива символов. Он должен иметь достаточное количество элементов для размещения скопированных символов или метода вызывает исключение ArgumentOutOfRangeException.

sourceIndex и destinationIndex отсчитываются от нуля.

См. также раздел

Применяется к