C# 中的 Insert() 方法


C# 中的 Insert() 方法用於返回一個新字串,其中指定字串插入本例項中指定索引位置。

語法

語法如下 -

public string Insert (int begnIndex, string val);

在上面,引數 begnIndex 是插入的從零開始的索引位置,而 val 是要插入的字串。

例項

現在讓我們看一個例項 -

 線上演示

using System;
public class Demo{
   public static void Main(){
      String str = "JohnWick";
      Console.WriteLine("Initial string = "+str);
      String res = str.Insert(5, " ");
      Console.WriteLine("Updated = "+res);
   }
}

輸出

這將產生以下輸出 -

Initial string = JohnWick
Updated = JohnW ick

例項

現在讓我們看另一個示例

 線上演示

using System;
public class Demo{
   public static void Main(){
      String str = "WelcomeJacob";
      Console.WriteLine("Initial string = "+str);
      String res = str.Insert(7, " here, ");
      Console.WriteLine("Updated = "+res);
   }
}

輸出

這將產生以下輸出 -

Initial string = WelcomeJacob
Updated = Welcome here, Jacob

更新於: 2019 年 12 月 3 日

752 人觀看

啟動你的 職業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.