C# 什么是String.CopyTo?
有人能解释为什么这段代码的输出只是“hello”以及这段代码的含义吗C# 什么是String.CopyTo?,c#,C#,有人能解释为什么这段代码的输出只是“hello”以及这段代码的含义吗 ( 0, characterArray, 0, characterArray.Length ); 输出显示: 字符数组是:hello 守则如下: The character array is: hello string string1=“你好”; char[]characterArray=新字符[5]; string1.CopyTo(0,characterArray,0,characterArray.Length); Con
( 0, characterArray, 0, characterArray.Length );
输出显示:
字符数组是:hello
守则如下:
The character array is: hello
string string1=“你好”;
char[]characterArray=新字符[5];
string1.CopyTo(0,characterArray,0,characterArray.Length);
Console.Write(“\n字符数组为:”);
for(int i=0;i
这是因为您的数组仅设置为5个字符。将它扩展到11,它就会工作
以下是Copyto的含义:
string string1 = "hello there";
char[] characterArray = new char[ 5 ];
string1.CopyTo( 0, characterArray, 0, characterArray.Length );
Console.Write( "\nThe character array is: " );
for ( int i = 0; i < characterArray.Length; i++ )
Console.Write( characterArray[ i ] );
参数
源索引
类型:系统..::.Int32
此实例中的角色位置。
目的地
类型:数组[]()[]
Unicode字符的数组。
目的索引
类型:系统..::.Int32
目标中的数组元素。
计数
类型:系统..::.Int32
此实例中要复制到目标的字符数。
摘自:它只显示“hello”,因为您的字符数组只有5个字符长。至于CopyTo的参数,请阅读,这是因为您的字符数组大小只有5。如果您想将整个字符串作为一个数组,可以改为string.toCharray您的标题应该更明确地说明这个问题的实际含义。这是什么意思;
public void CopyTo(
int sourceIndex,
char[] destination,
int destinationIndex,
int count
)
Parameters
sourceIndex
Type: System..::.Int32
A character position in this instance.
destination
Type: array[]()[]
An array of Unicode characters.
destinationIndex
Type: System..::.Int32
An array element in destination.
count
Type: System..::.Int32
The number of characters in this instance to copy to destination.