Nyelv

String.CopyTo Metódus

Definíció

Túlterhelések

Név Description
CopyTo(Span<Char>)

Másolja a sztring tartalmát a céltartományba.

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

Egy megadott számú karaktert másol a példány egy megadott pozíciójából egy Unicode-karakterből álló tömbbe.

CopyTo(Span<Char>)

Forrás:
String.cs
Forrás:
String.cs
Forrás:
String.cs
Forrás:
String.cs
Forrás:
String.cs

Másolja a sztring tartalmát a céltartományba.

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))

Paraméterek

destination
Span<Char>

A sztring tartalmának másolási tartománya.

Kivételek

A céltartomány rövidebb, mint a forrássztring.

A következőre érvényes:

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

Forrás:
String.cs
Forrás:
String.cs
Forrás:
String.cs
Forrás:
String.cs
Forrás:
String.cs

Egy megadott számú karaktert másol a példány egy megadott pozíciójából egy Unicode-karakterből álló tömbbe.

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)

Paraméterek

sourceIndex
Int32

A példány első karakterének indexe a másoláshoz.

destination
Char[]

Unicode-karakterekből álló tömb, amelybe a példányban lévő karaktereket másolja a rendszer.

destinationIndex
Int32

Az index, amelyben destination a másolási művelet megkezdődik.

count
Int32

A példányban a másolandó destinationkarakterek száma.

Kivételek

destination az null.

sourceIndex, vagy destinationIndexcount negatív

-vagy-

sourceIndex nem azonosít pozíciót az aktuális példányban.

-vagy-

destinationIndex nem azonosít érvényes indexet a destination tömbben.

-vagy-

count nagyobb, mint a példánytól sourceIndex a végéig történő részszűrés hossza

-vagy-

countnagyobb, mint a tömb végéhez destinationIndex tartozó destination alarraság hossza.

Példák

Az alábbi példa a metódust CopyTo mutatja be.

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

Megjegyzések

Ez a metódus a példány pozíciójából a count karaktertömb pozíciójába sourceIndex másolja destinationIndex a destination karaktereket. Ez a metódus nem méretezi át a destination karaktertömböt; elegendő számú elemet kell tartalmaznia a másolt karakterek elhelyezéséhez, vagy a metódus egy ArgumentOutOfRangeException.

sourceIndex és destinationIndex nulla alapúak.

Lásd még

A következőre érvényes: