String.CopyTo Metod

Definition

Överlagringar

Name Description
CopyTo(Span<Char>)

Kopierar innehållet i strängen till målintervallet.

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

Kopierar ett angivet antal tecken från en angiven position i den här instansen till en angiven position i en matris med Unicode-tecken.

CopyTo(Span<Char>)

Källa:
String.cs
Källa:
String.cs
Källa:
String.cs
Källa:
String.cs
Källa:
String.cs

Kopierar innehållet i strängen till målintervallet.

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

Parametrar

destination
Span<Char>

Det intervall som strängens innehåll ska kopieras till.

Undantag

Målintervallet är kortare än källsträngen.

Gäller för

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

Källa:
String.cs
Källa:
String.cs
Källa:
String.cs
Källa:
String.cs
Källa:
String.cs

Kopierar ett angivet antal tecken från en angiven position i den här instansen till en angiven position i en matris med Unicode-tecken.

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)

Parametrar

sourceIndex
Int32

Indexet för det första tecknet i den här instansen som ska kopieras.

destination
Char[]

En matris med Unicode-tecken som tecken i den här instansen kopieras till.

destinationIndex
Int32

Indexet destination där kopieringsåtgärden påbörjas.

count
Int32

Antalet tecken i den här instansen som ska kopieras till destination.

Undantag

destination är null.

sourceIndex, destinationIndex, eller count är negativ

-eller-

sourceIndex identifierar inte någon position i den aktuella instansen.

-eller-

destinationIndex identifierar inte ett giltigt index i matrisen destination .

-eller-

count är större än längden på delsträngen från sourceIndex till slutet av den här instansen

-eller-

count är större än längden på underarrayen från destinationIndex till slutet av matrisen destination .

Exempel

I följande exempel visas CopyTo metoden.

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

Kommentarer

Den här metoden kopierar count tecken från positionen för den sourceIndex här instansen till positionen för destinationIndex teckenmatrisendestination. Den här metoden ändrar inte storlek på teckenmatrisen destination . Den måste ha tillräckligt många element för att rymma de kopierade tecknen eller så genererar metoden en ArgumentOutOfRangeException.

sourceIndex och destinationIndex är nollbaserade.

Se även

Gäller för