c#copyto_String.CopyTo()方法以及C#中的示例

c#copyto

C#String.CopyTo()方法 (C# String.CopyTo() Method)

String.CopyTo() method is used to copy a specified number of characters from given indexes of the string to the specified position in a character array.

String.CopyTo()方法用于将指定数量的字符从给定的字符串索引复制到字符数组中的指定位置。

Syntax:

句法:

    public void CopyTo (int source_index, 
        char[] destination, 
        int destination_index, 
        int count);

Parameter:

参数:

  • source_index - index to the string from where you want to copy the string

    source_index-要从中复制字符串的字符串的索引

  • destination - target character array in which you want to copy the part of the string

    destination-您要在其中复制字符串部分的目标字符数组

  • destination_index - index in the targeted character array

    destination_index-目标字符数组中的索引

  • count - total number of characters to be copied in character array

    count-要在字符数组中复制的字符总数

Return value: void - It returns nothing.

返回值: void-不返回任何内容。

Example:

例:

    Input:
    string str = "Hello world!";
    char[] arr = { 'I', 'n', 'c', 'l', 'u', 'd', 'H', 'e', 'l', 'p' };
    
    copying "Hello " to arr:
    str.CopyTo(0, arr, 0, 6);

    Output:
    str = Hello world!
    arr = Hello Help

C#使用String.CopyTo()方法将字符从字符串复制到字符数组的示例 (C# Example to copy a characters from string to characters array using String.CopyTo() method)

using System;
using System.Text;

namespace Test
{
    class Program
    {
        static void printCharArray(char[] a){
            foreach (char item in a)
            {
                Console.Write(item);
            }
        }

        static void Main(string[] args)
        {
            string str = "Hello world!";
            char[] arr = { 'I', 'n', 'c', 'l', 'u', 'd', 'H', 'e', 'l', 'p' };

            //printing values
            Console.WriteLine("Before CopyTo...");
            Console.WriteLine("str = " + str);
            Console.Write("arr = ");
            printCharArray(arr);
            Console.WriteLine();

            //copying "Hello " to arr
            str.CopyTo(0, arr, 0, 6);

            //printing values
            Console.WriteLine("After CopyTo 1)...");
            Console.WriteLine("str = " + str);
            Console.Write("arr = ");
            printCharArray(arr);
            Console.WriteLine();


            //copying "World! " to arr
            str.CopyTo(6, arr, 0, 6);

            //printing values
            Console.WriteLine("After CopyTo 1)...");
            Console.WriteLine("str = " + str);
            Console.Write("arr = ");
            printCharArray(arr);
            Console.WriteLine();

            //hit ENTER to exit
            Console.ReadLine();
        }
    }
}

Output

输出量

Before CopyTo...
str = Hello world!
arr = IncludHelp
After CopyTo 1)...
str = Hello world!
arr = Hello Help
After CopyTo 1)...
str = Hello world!
arr = world!Help

Reference: String.CopyTo(Int32, Char[], Int32, Int32) Method

参考: String.CopyTo(Int32,Char [],Int32,Int32)方法

翻译自: https://www.includehelp.com/dot-net/string-copyto-method-with-example-in-c-sharp.aspx

c#copyto

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值