C# String.CopyTo() 方法
String.CopyTo() 方法用於將指定數量的字符從字符串的給定索引複製到字符數組中的指定位置。
用法:
public void CopyTo (int source_index, char[] destination, int destination_index, int count);
參數:
source_index
- 索引到要從哪裏複製字符串的字符串destination
- 要在其中複製字符串部分的目標字符數組destination_index
- 目標字符數組中的索引count
- 要在字符數組中複製的字符總數
返回值: void
- 它什麽都不返回。
例:
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
使用 String.CopyTo() 方法將字符從字符串複製到字符數組的 C# 示例
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();
}
}
}
輸出
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
參考:String.CopyTo(Int32, Char[], Int32, Int32) 方法
相關用法
- C# String.Copy()用法及代碼示例
- C# String.Contains()用法及代碼示例
- C# String.Compare()用法及代碼示例
- C# String.Clone()用法及代碼示例
- C# String.ToUpperInvariant用法及代碼示例
- C# String.ToLowerInvariant用法及代碼示例
- C# String.IsNullOrEmpty()用法及代碼示例
- C# String.ToCharArray()用法及代碼示例
- C# String.Insert()用法及代碼示例
- C# String.Split()用法及代碼示例
- C# String.Format()函數用法及代碼示例
- C# String.Format()方法用法及代碼示例
- C# String.IsNormalized用法及代碼示例
- C# String.IndexOf()方法用法及代碼示例
- C# StringBuilder.ToString用法及代碼示例
- C# String ToLower()用法及代碼示例
- C# String ToString()用法及代碼示例
- C# String Contains()用法及代碼示例
- C# String ToCharArray()用法及代碼示例
- C# String IndexOf()用法及代碼示例
注:本文由純淨天空篩選整理自 String.CopyTo() method with example in C#。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。