zoukankan      html  css  js  c++  java
  • 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();
  • 相关阅读:
    原生js 异步请求,responseXML解析
    asp.net中Page.ClientScript.RegisterStartupScript用法小结
    asp.net 在repeater控件中加按钮
    无法打开物理文件 操作系统错误 5:拒绝访问 SQL Sever
    js 注册控件的onclick事件
    js控件设置只读属性和不可用属性
    js CheckBox只读
    js时间日期格式
    js正则判断日期
    UIPickerView的使用(三)
  • 原文地址:https://www.cnblogs.com/pengyouqiang88/p/5125428.html
Copyright © 2011-2022 走看看