สมมติว่าสตริงคือ −
string str = "Never Give Up!";
ขั้นแรก แยกแต่ละคำ -
string[] strSplit = str.Split();
ตอนนี้ วนซ้ำแต่ละคำและใช้วิธีการย่อยเพื่อแสดงตัวอักษรตัวแรกตามที่แสดงในรหัสต่อไปนี้ -
ตัวอย่าง
using System;
public class Program {
public static void Main() {
string str = "Never Give Up!";
Console.WriteLine("Initial String= "+str);
Console.WriteLine("Displaying first letter of each word...");
string[] strSplit = str.Split();
foreach (string res in strSplit) {
Console.Write(res.Substring(0,1));
}
}
} ผลลัพธ์
Initial String= Never Give Up! Displaying first letter of each word... NGU