Thursday 23 August 2012

String Copy() and CopyTo() in C#

Example on String Copy() and CopyTo() Method.

Copy() :

CSharp String Copy method is create a new String object with the same content.
Syntax: string string.Copy(string str)


Parameters:
String str : The argument String for Copy method.




CopyTo() :

CSharp string CopyTo method Copies a specified number of characters from a specified position in this instance to a specified position in an array of characters.

Syntax: void string.CopyTo(int sourceIndex,char[] destination,  int destinationindex,int count)

Parameters:
int sourceIndex : The starting position of the source String
char[] destination : The character Array
int destinationindex : Array element in the destination
int count : The number of characters to destination


public class StringCopy&CopyToExample
      {
            public static void Main()
            {
                  //Copy() Methode
                  string strCopy1 = null;
                  string strCopy2 = null;

                  strCopy1 = "C# Copy() Method test";
                  strCopy2 = string.Copy(strCopy1);
                  Console.WriteLine(strCopy2);
                  //MessageBox.Show(str2);
                 
                  //CopyTo() Methode
                  string strTo = "C#CopyTo() sample";
                  char[] chrs = new char[5];
                  strTo.CopyTo(0, chrs, 0, 5);
                 
                  Console.WriteLine(chrs[0].ToString() + chrs[1].ToString() + chrs[2].ToString()+ chrs[3].ToString()  + chrs[4].ToString() + chrs[5].ToString());
           
                  Console.ReadLine();
            }
      }

No comments:

Post a Comment