Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to convert string to title case in C#?
Title case is any text, such as in a title or heading, where the first letter of major words is capitalized. Title case or headline case is a style of capitalization used for rendering the titles of published works or works of art in English. When using title case, all words are capitalized except for minor words (like articles, prepositions, and conjunctions) unless they are the first or last word of the title.
C# provides the ToTitleCase method through the TextInfo class to convert strings to title case format. This method capitalizes the first letter of each word while maintaining the original string length.
Syntax
Following is the syntax for converting a string to title case −
TextInfo textInfo = CultureInfo.CurrentCulture.TextInfo; string titleCase = textInfo.ToTitleCase(inputString);
You can also specify a specific culture −
TextInfo textInfo = new CultureInfo("en-US", false).TextInfo;
string titleCase = textInfo.ToTitleCase(inputString);
Using ToTitleCase with Mixed Case Input
Example
using System;
using System.Globalization;
class Program {
static void Main(string[] args) {
string myString = "wAr aNd pEaCe";
TextInfo myTI = new CultureInfo("en-US", false).TextInfo;
Console.WriteLine(""{0}" to titlecase: {1}", myString, myTI.ToTitleCase(myString));
}
}
The output of the above code is −
"wAr aNd pEaCe" to titlecase: War And Peace
Converting Multiple Strings to Title Case
Example
using System;
using System.Globalization;
class Program {
static void Main(string[] args) {
string[] values = {
"a tale of three cities", "gROWL rescue",
"inside the office", "sports and tennis",
"The Return of Christmas Holmes"
};
TextInfo ti = CultureInfo.CurrentCulture.TextInfo;
foreach (var value in values)
Console.WriteLine("{0} --> {1}", value, ti.ToTitleCase(value));
}
}
The output of the above code is −
a tale of three cities --> A Tale Of Three Cities gROWL rescue --> Growl Rescue inside the office --> Inside The Office sports and tennis --> Sports And Tennis The Return of Christmas Holmes --> The Return Of Christmas Holmes
Using ToTitleCase with Lowercase Conversion
The ToTitleCase method works best when the input string is already in lowercase. For mixed-case or uppercase strings, convert to lowercase first for consistent results −
Example
using System;
using System.Globalization;
class Program {
static void Main(string[] args) {
string[] testStrings = {
"HELLO WORLD",
"hELLo WoRLD",
"hello world"
};
TextInfo ti = CultureInfo.CurrentCulture.TextInfo;
foreach (string str in testStrings) {
Console.WriteLine("Original: {0}", str);
Console.WriteLine("Direct ToTitleCase: {0}", ti.ToTitleCase(str));
Console.WriteLine("Lowercase then ToTitleCase: {0}", ti.ToTitleCase(str.ToLower()));
Console.WriteLine();
}
}
}
The output of the above code is −
Original: HELLO WORLD Direct ToTitleCase: HELLO WORLD Lowercase then ToTitleCase: Hello World Original: hELLo WoRLD Direct ToTitleCase: HELLo WoRLD Lowercase then ToTitleCase: Hello World Original: hello world Direct ToTitleCase: Hello World Lowercase then ToTitleCase: Hello World
Key Points
ToTitleCaseonly capitalizes the first letter of words that are entirely lowercase.Words that are already mixed-case or uppercase remain unchanged.
For consistent results, convert the input to lowercase before applying
ToTitleCase.The method respects culture-specific rules when a specific culture is provided.
Conclusion
The ToTitleCase method in C# provides an easy way to convert strings to title case format using the TextInfo class. For best results with mixed-case input, convert the string to lowercase first before applying ToTitleCase.
