當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C# String.Copy()用法及代碼示例

C# String.Copy() 方法

String.Copy() 方法用於將字符串複製到新的字符串對象,它返回與給定字符串具有相同值的字符串的新實例。

用法:

    public static string Copy (string str); 

參數:字符串 – 要複製其值

返回值: string- 返回給定強的副本。

例:

    Input:
    string str1 = "IncludeHelp";
    
    creating new string using String.Copy() method:
    string str2 = String.Copy(str1);

    Output:
    str1:IncludeHelp
    str2:IncludeHelp

使用 String.Copy() 方法複製字符串的 C# 示例

using System;
using System.Text;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            string str1 = "IncludeHelp";

            //creating new string using String.Copy() method
            string str2 = String.Copy(str1);

            //printing strings
            Console.WriteLine("str1:" + str1);
            Console.WriteLine("str2:" + str2);

            //hit ENTER to exit
            Console.ReadLine();
        }
    }
}

輸出

str1:IncludeHelp
str2:IncludeHelp

參考:String.Copy(String) 方法



相關用法


注:本文由純淨天空篩選整理自 String.Copy() method with example in C#。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。