C# StringBuilder CopyTo

Description

StringBuilder CopyTo Copies the characters from a specified segment of this instance to a specified segment of a destination Char array.

Syntax

StringBuilder.CopyTo has the following syntax.


[ComVisibleAttribute(false)]/*from  w  w w  . ja  v a2  s. c  om*/
public void CopyTo(
  int sourceIndex,
  char[] destination,
  int destinationIndex,
  int count
)

Parameters

StringBuilder.CopyTo has the following parameters.

  • sourceIndex - The starting position in this instance where characters will be copied from. The index is zero-based.
  • destination - The array where characters will be copied.
  • destinationIndex - The starting position in destination where characters will be copied. The index is zero-based.
  • count - The number of characters to be copied.

Returns

StringBuilder.CopyTo method returns

Example

The following example demonstrates the CopyTo method.


//w ww. j a v a 2  s  .com
using System;
using System.Text;

class Sample 
{
    
    public static void Main() 
    {
        char[] dest = new char[6];
        StringBuilder src = new StringBuilder("abcdefghijklmnopqrstuvwxyz!");
        dest[1] = ')';
        dest[2] = ' ';
        Console.WriteLine("\nPiece) Data:");
        for(int ix = 0; ix < 9; ix++){
            dest[0] = ix.ToString()[0];
            src.CopyTo(ix * 3, dest, 3, 3);
            Console.WriteLine(dest);
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Text »




ASCIIEncoding
Encoding
EncodingInfo
StringBuilder
UnicodeEncoding
UTF8Encoding