Search code examples
c#stringtitle-case

Update Part of string to Title Case


I have a sequence of strings which I get in following format:

  • Project1:toyota:Corolla
  • Project1:Hoyota:Accord
  • Project1:Toyota:Camry

As you can see middle section of the string is not consistent case (for Corolloa, it is listed as toyota). I need to change above as follows:

  • Project1:Toyota:Corolla
  • Project1:Hoyota:Accord
  • Project1:Toyota:Camry

I want to make middle section of the string to be Title Case.

I am using following

static TextInfo textInfo = new CultureInfo( "en-US" ).TextInfo;

and using .ToTitleCase but the issue with TitleCase is if the string is in UPPERCASE, it would not change to TitleCase. Do we know how to handle a case when string is uppercase.


Solution

  • You can use .ToTitleCase()

    var myString = "Project1:toyota:Corolla";
    TextInfo textInfo = new CultureInfo( "en-US" ).TextInfo;
    myString = textInfo.ToTitleCase(myString);