Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

วิธีดึงตัวอักษร 4 ตัวสุดท้ายจากสตริงใน C#

ขั้นแรก กำหนดสตริง:

string str = "Football and Tennis";

ใช้เมธอด Substring() ร่วมกับ Length:

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