C# | CopyTo() Method

In C#, CopyTo() method is a string method. It is used to copy a specified number of characters from a specified position in the string and it copies the characters of this string into a array of Unicode characters.

Syntax:

public void CopyTo(int sourceIndex, char[] destination, 
                      int destinationIndex, int count)

Explanation: CopyTo() method will copied the count characters from the sourceIndex position to the destinationIndex position of destination character array. This method accepts four parameters as follows :



  1. sourceIndex : Index of String to be copied. And its type is System.Int32 .
  2. destination : It is the array of Unicode characters to which characters will be copied. Its type is System.Char[].
  3. destinationIndex : It is starting index of array from where the copy operation begins. Its type is System.Int32.
  4. count : It is the number of characters which will copy to destination. Its type is System.Int32.

Examples :

Input : str  = "GeeksForGeeks"
        char [] Copystring = new char[15];     
        str.CopyTo(5, Copystring, 0, 3);
Output: For

Input : str2 = "GeeksForGeeks";
        char [] Copystring = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd'};
        str2.CopyTo(8, Copystring, 6, 5);
Output: Hello Geeks

Below example programs illustrate the ToCopy() Method :

  • Example 1:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C# program to illustrate the
    // ToCopy() string method
    using System;
      
    class Geeks {
          
        // Main Method
        public static void Main()
        {
            string str = "GeeksForGeeks";
            char[] dest = new char[15];
      
            // str index 5 to 5+3 has to 
            // copy into Copystring
            // 3 is no. of character
            // 0 is start index of Copystring
            str.CopyTo(5, dest, 0, 3);
              
            // Displaying String
            Console.Write("The Copied String in dest Variable is: ");
            Console.WriteLine(dest);
        }
    }

    chevron_right

    
    

    Output:

    The Copied String in dest Variable is: For
    
  • Example 2:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C# program to illustrate the
    // ToCopy() string method
    using System;
      
    class Geeks {
          
        // Main Method
        public static void Main()
        {
            string str2 = "GeeksForGeeks";
            char[] dest = {'H', 'e', 'l', 'l', 'o', ' ',
                                 'W', 'o', 'r', 'l', 'd' };
      
            // str index 8 to 8 + 5 has 
            // to copy into Copystring
            // 5 is no of character
            // 6 is start index of Copystring
            str2.CopyTo(8, dest, 6, 5);
              
            // Displaying the result
            Console.Write("String Copied in dest is: ");
            Console.WriteLine(dest);
        }
    }

    chevron_right

    
    

    Output:

    String Copied in dest is: Hello Geeks
    

Note: If “destination” is null then it will cause an Exception as ArgumentNullException. There are different cases when exception occurs: ArgumentOutOfRangeException

cases 1: if sourceIndex, destinationIndex, or count is negative.
cases 2: if sourceIndex does not identify a position in the current instance.
cases 3: if destinationIndex does not identify a valid index in the destination array.
cases 4: if the count is greater than the length of the substring from startIndex to the end of this instance
cases 5: if the count is greater than the length of the subarray from destinationIndex to the end of the destination array.

Reference: https://msdn.microsoft.com/en-us/library/system.string.copyto



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.