![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# String CopyTo() Method
The CopyTo() method in C# is used to copy a specified number of characters from a specified position in this instance to a specified position in an array of Unicode characters.
Syntax
public void CopyTo (int srcIndex, char[] dest, int desIndex, int count);
Above,
- srcIndex − Index of the first character in this instance to copy.
- dest − Array of Unicode characters to which characters in this instance are copied.
- destIndex − The index in destination at which the copy operation begins.
- Count − Number of characters in this instance to copy to destination.
Example
Let us now see an example -
using System; public class Demo { public static void Main() { string str = "JohnAndJacob"; Console.WriteLine("String = "+str); char[] destArr = new char[20]; str.CopyTo(1, destArr, 0, 4); Console.Write(destArr); } }
Output
String = JohnAndJacob ohnA
Example
Let us now see another example -
using System; public class Demo { public static void Main() { string str = "JohnAndJacob"; Console.WriteLine("String = "+str); char[] destArr = new char[20]; destArr[0] = 'A'; destArr[1] = 'B'; destArr[2] = 'C'; destArr[3] = 'D'; Console.WriteLine(destArr); str.CopyTo(2, destArr, 3, 4); Console.Write(destArr); } }
Output
This will produce the following output -
String = JohnAndJacob ABCD ABChnAn
Advertisements