C# String CopyTo

Description

String CopyTo copies a specified number of characters from a specified position in this instance to a specified position in an array of Unicode characters.

Syntax

String.CopyTo has the following syntax.


public void CopyTo(
  int sourceIndex,
  char[] destination,
  int destinationIndex,
  int count//from w w w  .j  a  va 2 s. c o m
)

Parameters

String.CopyTo has the following parameters.

  • sourceIndex - The index of the first character in this instance to copy.
  • destination - An array of Unicode characters to which characters in this instance are copied.
  • destinationIndex - The index in destination at which the copy operation begins.
  • count - The number of characters in this instance to copy to destination.

Returns

String.CopyTo method returns

Example

The following example demonstrates the CopyTo method.


/*from   w ww  . ja  va  2 s  .c  om*/
using System;

public class CopyToTest {
    public static void Main() {
        string strSource = "changed";
        char [] destination = { 'T', 'h', 'e', ' ', 'i', 'n', 'i', 't', 'i', 'a', 'l', ' ',
                'a', 'r', 'r', 'a', 'y' };

        strSource.CopyTo ( 0, destination, 4, strSource.Length );
        Console.WriteLine( destination );

        strSource = "A different string";

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

        Console.WriteLine( destination );
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version