ขั้นแรก ตั้งค่าสตริง −
string str = "Football and Tennis";
ตอนนี้ ใช้เมธอด substring() เพื่อรับ 4 ตัวอักษรสุดท้าย -
str.Substring(str.Length - 4);
ให้เราดูรหัสที่สมบูรณ์ -
ตัวอย่าง
using System;
public class Demo {
public static void Main() {
string str = "Football and Tennis";
string res = str.Substring(str.Length - 4);
Console.WriteLine(res);
}
} ผลลัพธ์
nnis