各位高手!请教一下C#中复制字符串copy to 方法怎们用!最好能有模版!小弟不胜感激!

解决方案 »

  1.   

    String str1 = "abc";
            String str2 = "xyz";
            str2 = String.Copy(str1);
      

  2.   


    一、String.CopyTo 方法 
    将指定数目的字符从此实例中的指定位置复制到 Unicode 字符数组中的指定位置。
    语法:
    public void CopyTo (
     int sourceIndex,
     char[] destination,
     int destinationIndex,
     int count
    )
    参数
    sourceIndex
    此实例中的字符位置。 destination
    Unicode 字符的数组。 destinationIndex
    destination 中的数组元素。 count
    此实例中要复制到 destination 的字符数。
    示例:
    using System;public class CopyToTest {
        public static void Main() {        // Embed an array of characters in a string
            string strSource = "changed";
        char [] destination = { 'T', 'h', 'e', ' ', 'i', 'n', 'i', 't', 'i', 'a', 'l', ' ',
                    'a', 'r', 'r', 'a', 'y' };        // Print the char array
            Console.WriteLine( destination );        // Embed the source string in the destination string
            strSource.CopyTo ( 0, destination, 4, strSource.Length );        // Print the resulting array
            Console.WriteLine( destination );        strSource = "A different string";        // Embed only a section of the source string in the destination
            strSource.CopyTo ( 2, destination, 3, 9 );        // Print the resulting array
            Console.WriteLine( destination );
        }
    }二、String.Copy 方法 
    创建一个与指定的 String 具有相同值的 String 的新实例。 命名空间:System
    程序集:mscorlib(在 mscorlib.dll 中)语法
    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'
    */