Reputation: 3527
First, all my cities were returned as UPPERCASE, so I switched them to lowercase. How can I get the first letter as uppercase now? Thanks for any help!
List<string> cities = new List<string>();
foreach (DataRow row in dt.Rows)
{
cities.Add(row[0].ToString().ToLower());
**ADDED THIS BUT NOTHING HAPPENED**
CultureInfo.CurrentCulture.TextInfo.ToTitleCase(row[0] as string);
}
return cities;
Upvotes: 8
Views: 11293
Reputation: 6317
I know I'm resurrecting a ghost here, but I had the same problem, and wanted to share what I think is the best solution. There are a few ways you can do it, either splitting the string and replacing the first letter, or transforming it into a char-array for better performance. The best performance, though, comes with using a regular expression.
You can use a bit of Regex voodoo to find the first letter of each word. The pattern you are looking for is \b\w (\b means the beginning of a word, and \w is an alpha character). Use a MatchEvaluator delegate (or an equivalent lambda expression) to modify the string (the first character, that your pattern found).
Here's an extension method over string that will upper-case-ify the first letter of each word in a string:
static string UpperCaseFirst(this string input)
{
return Regex.Replace(input, @"\b\w", (Match match)=> match.ToString().ToUpper())
}
Upvotes: 4
Reputation: 41
public static string UppercaseFirst(string value)
{
// Check for empty string.
if (string.IsNullOrEmpty(value))
{
return string.Empty;
}
// Return char and concat substring.
return char.ToUpper(value[0]) + value.Substring(1);
}
cities.Select(UppercaseFirst).ToList();
Upvotes: 0
Reputation:
Here's quick little method:
public string UpperCaseFirstLetter(string YourLowerCaseWord)
{
if (string.IsNullOrEmpty(YourLowerCaseWord))
return string.Empty;
return char.ToUpper(YourLowerCaseWord[0]) + YourLowerCaseWord.Substring(1);
}
Upvotes: 0
Reputation: 13507
Use the TextInfo.ToTitleCase method:
System.Globalization.TextInfo.ToTitleCase();
A bit from the MSDN example, modified to work with OP's code:
// Defines the string with mixed casing.
string myString = row[0] as String;
// Creates a TextInfo based on the "en-US" culture.
TextInfo myTI = new CultureInfo("en-US", false).TextInfo;
// Retrieve a titlecase'd version of the string.
string myCity = myTI.ToTitleCase(myString);
All in one line:
string myCity = new CultureInfo("en-US", false).TextInfo.ToTitleCase(row[0] as String);
Upvotes: 19
Reputation: 392
Regex may seem a bit long, but works
List<string> cities = new List<string>();
foreach (DataRow row in dt.Rows)
{
string city = row[0].ToString();
cities.Add(String.Concat(Regex.Replace(city, "([a-zA-Z])([a-zA-Z]+)", "$1").ToUpper(System.Globalization.CultureInfo.InvariantCulture), Regex.Replace(city, "([a-zA-Z])([a-zA-Z]+)", "$2").ToLower(System.Globalization.CultureInfo.InvariantCulture)));
}
return cities;
Upvotes: 3
Reputation: 8166
here is an extension method that you can use. It supports the current culture, or allows you to pass in the culture.
to use:
cities.Add(row[0].ToString().ToTitleCase()
public static class StringExtension
{
/// <summary>
/// Use the current thread's culture info for conversion
/// </summary>
public static string ToTitleCase(this string str)
{
var cultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture;
return cultureInfo.TextInfo.ToTitleCase(str.ToLower());
}
/// <summary>
/// Overload which uses the culture info with the specified name
/// </summary>
public static string ToTitleCase(this string str, string cultureInfoName)
{
var cultureInfo = new CultureInfo(cultureInfoName);
return cultureInfo.TextInfo.ToTitleCase(str.ToLower());
}
/// <summary>
/// Overload which uses the specified culture info
/// </summary>
public static string ToTitleCase(this string str, CultureInfo cultureInfo)
{
return cultureInfo.TextInfo.ToTitleCase(str.ToLower());
}
}
Upvotes: 1
Reputation: 151586
You could use this method (or create an extension method out of it):
static string UpperCaseFirst(this string s)
{
// Check for empty string.
if (string.IsNullOrEmpty(s))
{
return string.Empty;
}
// Return char and concat substring.
return char.ToUpper(s[0]) + s.Substring(1);
}
Upvotes: 0
Reputation: 7228
With linq:
String newString = new String(str.Select((ch, index) => (index == 0) ? ch : Char.ToLower(ch)).ToArray()); *
Upvotes: 0