柳树的花是雌雄同株:C#中的字符串复制函数是什么?

来源:百度文库 编辑:神马品牌网 时间:2024/05/06 04:17:07
要将str1的复制到str2中去,怎么写?
这不是比较嘛?
要加using System.***吗?

你应该在MSDN里面找方法啊!
一、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'
*/

上面这些都是MSDN里的,别懒,还是把MSDN装上吧!装上后自己去看String 类的方法就OK了!

Array中有个Clone方法

STRCPY