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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP