Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Chuỗi Title Case (Viết hoa chữ cái đầu) trong C#

Phương thức ToTitleCase viết hoa chữ cái đầu mỗi từ. Title case = viết hoa chữ cái đầu các từ chính.

Ví dụ

using System;
using System.Globalization;

class Demo {
    static void Main() {
        string str = "jack sparrow";
        string res = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(str);
        Console.WriteLine(res); // Jack Sparrow
    }
}

Kết quả

Jack Sparrow

Dùng CultureInfo.TextInfo cung cấp thông tin casing theo culture:

CultureInfo.CurrentCulture.TextInfo.ToTitleCase(str);