วิธีการ EndsWith() ใน C# ใช้เพื่อตรวจสอบว่าจุดสิ้นสุดของอินสแตนซ์สตริงปัจจุบันตรงกับสตริงที่ระบุหรือไม่
ไวยากรณ์
ต่อไปนี้เป็นไวยากรณ์ -
public bool EndsWith(string str)
ด้านบน พารามิเตอร์ str คือสตริงที่จะเปรียบเทียบ
ตัวอย่าง
ให้เรามาดูตัวอย่างการใช้งาน EndsWith() method −
using System;
public class Demo{
public static void Main(){
bool val;
string str = "demo$";
val = str.EndsWith("$");
Console.WriteLine("Return Value = "+val.ToString());
}
} ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
Return Value = True
ตัวอย่าง
ให้เรามาดูตัวอย่างการใช้งาน EndsWith() อีกตัวอย่างหนึ่ง -
using System;
public class Demo{
public static void Main(){
bool val;
string str = "mytext@";
val = str.EndsWith("#");
Console.WriteLine("Return Value = "+val.ToString());
}
} ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
Return Value = False