6

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;
Trey Copeland
  • 3,387
  • 7
  • 29
  • 46

9 Answers9

18

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);
Nahydrin
  • 13,197
  • 12
  • 59
  • 101
4

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())
}
Assaf Stone
  • 6,309
  • 1
  • 34
  • 43
3

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;
kochobay
  • 392
  • 1
  • 7
2
new CultureInfo("en-US",false).TextInfo.ToTitleCase(myString);
BFree
  • 102,548
  • 21
  • 159
  • 201
1

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());
    }
}
Anthony Shaw
  • 8,146
  • 4
  • 44
  • 62
0

With linq:

String newString = new String(str.Select((ch, index) => (index == 0) ? ch : Char.ToLower(ch)).ToArray()); *
Community
  • 1
  • 1
Orkun
  • 6,998
  • 8
  • 56
  • 103
0

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);
}
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
0

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);
}
0
    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();

Xzatrox
  • 31
  • 3