w3resource

C#: Demonstrates the CopyTo method

C# Sharp String: Exercise-40 with Solution

Write a C# Sharp program to demonstrates the CopyTo method.

Sample Solution:-

C# Sharp Code:

using System;

public class CopyToExample40 {
    public static void Main() {

        // Embed an array of characters in a string
        string strSource = "Python";
        // Declare a character array
        char[] destination = { 'w', '3', 'r', 'e', 's', 'o', 'u', 'r', 'c', 'e', ' ', 'C',
                'S', 'h', 'a', 'r', 'p', ' ', 'T', 'u', 't', 'o', 'r', 'i', 'a', 'l' };

        // Print the char array
        Console.WriteLine(destination);

        // Embed the source string in the destination string
        strSource.CopyTo(0, destination, 11, 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, 10, 9);

        // Print the resulting array
        Console.WriteLine(destination);
    }
}

Sample Output:

w3resource CSharp Tutorial                                                                                    
w3resource Python Tutorial                                                                                    
w3resourcedifferentutorial

Flowchart :

Flowchart: C# Sharp Exercises - Demonstrates the CopyTo method.

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a C# Sharp program to creates two string objects with different values. When it calls the Copy method to assign the first value to the second string, the output indicates that the strings represent different object references although their values are now equal. On the other hand, when the first string is assigned to the second string, the two strings have identical values because they represent the same object reference.
Next: Write a C# Sharp program to indicate whether each string in an array ends with a period (".").

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.