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

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

 

转载于:https://www.cnblogs.com/pengyouqiang88/p/5125428.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值