Giả sử chuỗi là -
string str = "Never Give Up!";
Đầu tiên, tách từng từ -
string[] strSplit = str.Split();
Bây giờ, lặp lại từng từ và sử dụng phương thức chuỗi con để hiển thị chữ cái đầu tiên như được hiển thị trong đoạn mã sau -
Ví dụ
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));
}
}
} Đầu ra
Initial String= Never Give Up! Displaying first letter of each word... NGU