0

The following example was mentioned in below URL

https://learn.microsoft.com/en-us/dotnet/standard/base-types/changing-case

public class Example
{
   public static void Main()
   {
      string[] values = { "a tale of two cities", "gROWL to the rescue",
                          "inside the US government", "sports and MLB baseball",
                          "The Return of Sherlock Holmes", "UNICEF and children"};

      TextInfo ti = CultureInfo.CurrentCulture.TextInfo;
      foreach (var value in values)
         Console.WriteLine("{0} --> {1}", value, ti.ToTitleCase(value));
   }
}
// The example displays the following output:
//    a tale of two cities --> A Tale Of Two Cities
//    gROWL to the rescue --> Growl To The Rescue
//    inside the US government --> Inside The US Government
//    sports and MLB baseball --> Sports And MLB Baseball
//    The Return of Sherlock Holmes --> The Return Of Sherlock Holmes
//    UNICEF and children --> UNICEF And Children

This is their NOTE

Note that although it is culture-sensitive, the TextInfo.ToTitleCase method does not provide linguistically correct casing rules. For instance, in the previous example, the method converts "a tale of two cities" to "A Tale Of Two Cities". However, the linguistically correct title casing for the en-US culture is "A Tale of Two Cities."

So In my app I used to following code to generate ToTitleCase()

public static string ConvertToTitleCase(string text)
{
    try
    {
        TextInfo textInfo = new CultureInfo("en-US", true).TextInfo;
        return textInfo.ToTitleCase(text);
    }
    catch (Exception)
    {

        throw;
    }
}

When I pass the "UNICEF and children" to it, it gives me the output as Unicef And Children. How to avoid titlecasing if we type a word which have CAPITAL LETTERS. also not capitalize words like "of"

Prageeth Liyanage
  • 1,612
  • 2
  • 19
  • 41

0 Answers0