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 HelpC#使用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 valuesConsole.WriteLine("Before CopyTo...");Console.WriteLine("str = " + str);Console.Write("arr = ");printCharArray(arr);Console.WriteLine();//copying "Hello " to arrstr.CopyTo(0, arr, 0, 6);//printing valuesConsole.WriteLine("After CopyTo 1)...");Console.WriteLine("str = " + str);Console.Write("arr = ");printCharArray(arr);Console.WriteLine();//copying "World! " to arrstr.CopyTo(6, arr, 0, 6);//printing valuesConsole.WriteLine("After CopyTo 1)...");Console.WriteLine("str = " + str);Console.Write("arr = ");printCharArray(arr);Console.WriteLine();//hit ENTER to exitConsole.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!HelpReference: 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
總結
以上是生活随笔為你收集整理的c#copyto_String.CopyTo()方法以及C#中的示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 求阶乘的第一个非零数字_查找数字阶乘中的
- 下一篇: c#c#继承窗体_C#继承能力问题和解答