[英]What is String.CopyTo?
Can any one explain why the output of this code is only 'hello' and what this code means? 谁能解释为什么此代码的输出仅为“ hello”以及此代码的含义?
( 0, characterArray, 0, characterArray.Length );
The output is showing: 输出显示:
The character array is: hello
The code follows: 代码如下:
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 ] );
It's because your array is only set for 5 characters. 这是因为您的数组仅设置为5个字符。 Expand it to 11 and it will work. 将其扩展到11即可使用。
Here is what the Copyto is: 这是Copyto:
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.
Taken from: http://msdn.microsoft.com/en-us/library/system.string.copyto.aspx 摘自: http : //msdn.microsoft.com/zh-cn/library/system.string.copyto.aspx
It's only showing 'hello' because your character array is only 5 chars long. 它仅显示“ hello”,因为您的字符数组只有5个字符。 As for the parameters to CopyTo, read http://msdn.microsoft.com/en-us/library/system.string.copyto.aspx 至于CopyTo的参数,请阅读http://msdn.microsoft.com/zh-cn/library/system.string.copyto.aspx
这是因为您的字符数组大小仅为5。如果您希望将整个字符串作为数组,则可以string.ToCharArray代替。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.