当前位置: 首页 > ds >正文

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 Help

C#使用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 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();
}
}
}

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!Help

Reference: 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

http://www.xdnf.cn/news/11646.html

相关文章:

  • 全方位了解IC芯片测试流程,IC芯片自动化测试平台分享
  • ALShow-带有漂亮皮肤的免费视频播放器(简约、华丽)
  • 串口通讯RS232、RS422、RS485详解
  • SSH简介及两种远程登录的方法_ssh登陆
  • 如何在hisi平台里面启用httpd-web服务器
  • C#开发之——StreamWriter(10.9)
  • 【5G学习】小区搜索——SSB
  • 基于FTP协议的文件上传与下载
  • assert_param函数的用法
  • 通过JAVA程序对ORACLE数据库进行操作并导出序列(SEQUENCE)
  • 常用搜索引擎 (转)
  • 社工必备查询网址汇总
  • 中国姓氏大全(常见508个,罕见740个)
  • STM32F429 19. RTC_实时时钟
  • “15个必备的自动化测试工具,助你构建2024年的自动化策略!“
  • 操作系统中的文件管理系统
  • 02 | 领域、子域、核心域、通用域和支撑域:傻傻分不清?
  • Wireshark 的抓包和分析,看这篇就够了!
  • 桌面虚拟化(VDI)的概述
  • TImage、TPaintBox、TPicture、TBitmap、TCanvas、TGraphic 的关系与区别
  • 使用ISAPI_Rewrite做实用的重定向
  • 蓝牙(Bluetooth)系统的无线接入
  • 视频网站提取原始地址之二——土豆
  • 数组知识点(详解)
  • JAF简介
  • 「DDD建模」DDD编程方法
  • 国标(GB)安全帽、安全救援头盔标准分享
  • SQL Server 入门学习总结---基础篇
  • jmeter函数---_setProperty 和 _property
  • 电竞玩家的云端盛宴!四大云电脑平台:ToDesk、顺网云、青椒云、极云普惠云实测大比拼...