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

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐