Home » C#.Net

String.CopyTo() method with example in C#

C# String.CopyTo() method: Here, we are going to learn about the CopyTo() method of String class with example.
Submitted by IncludeHelp, on March 19, 2019

C# String.CopyTo() Method

String.CopyTo() method is used to copy a specified number of characters from given indexes of the string to the specified position in a character array.

Syntax:

    public void CopyTo (int source_index, 
        char[] destination, 
        int destination_index, 
        int count);

Parameter:

  • source_index - index to the string from where you want to copy the string
  • destination - target character array in which you want to copy the part of the string
  • destination_index - index in the targeted character array
  • count - total number of characters to be copied in character array

Return value: void - It returns nothing.

Example:

    Input:
    string str = "Hello world!";
    char[] arr = { 'I', 'n', 'c', 'l', 'u', 'd', 'H', 'e', 'l', 'p' };
    
    copying "Hello " to arr:
    str.CopyTo(0, arr, 0, 6);

    Output:
    str = Hello world!
    arr = Hello Help

C# Example to copy a characters from string to characters array using String.CopyTo() method

using System;
using System.Text;

namespace Test
{
    class Program
    {
        static void printCharArray(char[] a){
            foreach (char item in a)
            {
                Console.Write(item);
            }
        }

        static void Main(string[] args)
        {
            string str = "Hello world!";
            char[] arr = { 'I', 'n', 'c', 'l', 'u', 'd', 'H', 'e', 'l', 'p' };

            //printing values
            Console.WriteLine("Before CopyTo...");
            Console.WriteLine("str = " + str);
            Console.Write("arr = ");
            printCharArray(arr);
            Console.WriteLine();

            //copying "Hello " to arr
            str.CopyTo(0, arr, 0, 6);

            //printing values
            Console.WriteLine("After CopyTo 1)...");
            Console.WriteLine("str = " + str);
            Console.Write("arr = ");
            printCharArray(arr);
            Console.WriteLine();


            //copying "World! " to arr
            str.CopyTo(6, arr, 0, 6);

            //printing values
            Console.WriteLine("After CopyTo 1)...");
            Console.WriteLine("str = " + str);
            Console.Write("arr = ");
            printCharArray(arr);
            Console.WriteLine();

            //hit ENTER to exit
            Console.ReadLine();
        }
    }
}

Output

Before CopyTo...
str = Hello world!
arr = IncludHelp
After CopyTo 1)...
str = Hello world!
arr = Hello Help
After CopyTo 1)...
str = Hello world!
arr = world!Help

Reference: String.CopyTo(Int32, Char[], Int32, Int32) Method






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.