C# 中 String.Copy() 和 String.CopyTo() 方法有什麼區別?


String.CopyTo() 方法獲取字串字元並將它們放入陣列中。將一組字元從源字串複製到字元陣列中。

以下為 Copy() 方法。-

示例

 線上演示

using System;
class Demo {

   static void Main(String[] args) {
      string str = "This is it!";
      char[] ch = new char[5];

      str.CopyTo(2, ch, 0, 2);

      Console.WriteLine("Output...");
      Console.WriteLine(ch);
   }
}

輸出

Output...
is

String.Copy() 建立具有類似內容的新字串物件。

示例

 線上演示

using System;
class Demo {
   static void Main(String[] args) {

      string str1 = "Welcome!";
      string str2 = "user";
      str2 = String.Copy(str1);

      Console.WriteLine("Output...");
      Console.WriteLine(str2);
   }
}

輸出

Output...
Welcome!

更新於: 6 月 22 日,2020

210 次瀏覽

開啟你的 職業

完成課程即可獲得認證

開始
廣告
© . All rights reserved.