如何在 C# 中將一個字串複製到另一個字串


要將一個字串複製到另一個字串,程式碼如下:-

示例

 即時演示

using System;
public class Demo {
   static public void Main(){
      string str1 = "Kevin";
      string str2 = String.Copy(str1);
      Console.WriteLine("String1 = "+str1);
      Console.WriteLine("String2 = "+str2);
   }
}

輸出

它會產生以下輸出 -

String1 = Kevin
String2 = Kevin

示例

現在讓我們看另一個示例:-

 即時演示

using System;
public class Demo {
   static public void Main(){
      string str1 = "Maisie";
      string str2 = "Ryan";
      Console.WriteLine("String1 (Before copying) = "+str1);
      Console.WriteLine("String2 (Before copying) = "+str2);
      str2 = String.Copy(str1);
      Console.WriteLine("String1 = "+str1);
      Console.WriteLine("String2 (Updated) = "+str2);
   }
}

輸出

它會產生以下輸出 -

String1 (Before copying) = Maisie String2 (Before copying) = Ryan
String1 = Maisie
String2 (Updated) = Maisie

更新於: 2019 年 12 月 10 日

387 次瀏覽

開啟職業生涯 生涯

完成本課程進行認證

開始
廣告
© . All rights reserved.