码迷,mamicode.com
首页 > 其他好文 > 详细

string类--复制《copy()与copyTo()》

时间:2016-01-12 21:25:44      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

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();

 

string类--复制《copy()与copyTo()》

标签:

原文地址:http://www.cnblogs.com/pengyouqiang88/p/5125428.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!