Csharp/CSharp Tutorial/String/String Copy
Copy a string
<source lang="csharp">using System;
class MainClass {
public static void Main() { string str1 = "ABCDEabcde1234567890"; string str2 = string.Copy(str1); string str3 = "asdf"; // compare strings if(str1 == str2) Console.WriteLine("str1 == str2"); else Console.WriteLine("str1 != str2"); if(str1 == str3) Console.WriteLine("str1 == str3"); else Console.WriteLine("str1 != str3"); }
}</source>
str1 == str2 str1 != str3
String CopyTo
<source lang="csharp">using System;
class Class1 { [STAThread] static void Main(string[] args) { string source = "ABCD"; char [] dest = { "1", "2", "3", "4", "5", "6", "7", "8" }; Console.Write( "Char array before = " ); Console.WriteLine( dest ); // copy substring into char array source.CopyTo( 0, dest, 4, source.Length ); Console.Write( "Char array after = " ); Console.WriteLine( dest ); // copy back into source string source = new String( dest ); Console.WriteLine( "New source = {0}\n", source ); } }</source>