gpt4 book ai didi

c# - 什么是 String.CopyTo?

转载 作者:行者123 更新时间:2023-11-30 20:14:34 30 4
gpt4 key购买 nike

谁能解释为什么这段代码的输出只是“hello”以及这段代码的含义?

( 0, characterArray, 0, characterArray.Length );

输出显示:

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 ] );

最佳答案

这是因为你的数组只设置了 5 个字符。将其扩展到 11 即可。

Copyto 是这样的:

public void CopyTo(
int sourceIndex,
char[] destination,
int destinationIndex,
int count
)
ParameterssourceIndexType: System..::.Int32A character position in this instance. destinationType: array[]()[]An array of Unicode characters. destinationIndexType: System..::.Int32An array element in destination. countType: System..::.Int32The number of characters in this instance to copy to destination. 

取自:http://msdn.microsoft.com/en-us/library/system.string.copyto.aspx

关于c# - 什么是 String.CopyTo?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/353569/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com