1、copy();
【语法】:
public static string Copy (string str); 参数--str 要复制的string 返回值--与str具有相同值的新string
// Sample for String.Copy() using System; class Sample { public static void Main() { string str1 = "abc"; string str2 = "xyz"; Console.WriteLine("1) str1 = ‘{0}‘", str1); Console.WriteLine("2) str2 = ‘{0}‘", str2); Console.WriteLine("Copy..."); str2 = String.Copy(str1); Console.WriteLine("3) str1 = ‘{0}‘", str1); Console.WriteLine("4) str2 = ‘{0}‘", str2); } } /* This example produces the following results: 1) str1 = ‘abc‘ 2) str2 = ‘xyz‘ Copy... 3) str1 = ‘abc‘ 4) str2 = ‘abc‘ */
2、copyTo();
【语法】:
public void CopyTo ( int sourceIndex, //为需要复制的字符起始位置 char[] destination, //为目标字符数组 int destinationIndex, //指定目标数组中的开始存放位置 int count //指定要复制的字符个数。 )
string strSource = "changed"; char[] destination = { ‘T‘, ‘h‘, ‘e‘, ‘ ‘, ‘i‘, ‘n‘, ‘i‘, ‘t‘, ‘i‘, ‘a‘, ‘l‘, ‘ ‘,‘a‘, ‘r‘, ‘r‘, ‘a‘, ‘y‘ }; Console.WriteLine(destination);//结果:The initial array strSource.CopyTo(0, destination, 4, strSource.Length); Console.WriteLine(destination);//结果:The changed array strSource = "A different string"; strSource.CopyTo(2, destination, 3, 9); Console.WriteLine(destination);//结果:Thedifferentarray Console.ReadKey();
时间: 2024-11-05 19:01:36