Insert() วิธีการใน C# ใช้เพื่อส่งคืนสตริงใหม่ที่มีการแทรกสตริงที่ระบุในตำแหน่งดัชนีที่ระบุในตัวอย่างนี้
ไวยากรณ์
ไวยากรณ์มีดังนี้ −
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