-1

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.

khar
  • 201
  • 4
  • 12
  • 1
    I think you mean either Proper/Title Case or PascalCase (both of these are the same as far as your examples are concerned). camelCase is when the first letter is lowercase. I've edit your question to reflect that. – Broots Waymb Mar 16 '18 at 20:41

3 Answers3

3

You could use TextInfo.ToTitleCase

textInfo.ToTitleCase("Project1:toyota:Corolla")
NotAnAuthor
  • 1,071
  • 8
  • 9
  • Thanks @NotAnAuthor. Do you know how to handle the case when string is in UPPERCASE ? TextInfo would always return string in uppercase. – khar Mar 19 '18 at 21:39
  • textInfo.ToTitleCase("Project1:TOYOTA:Corolla".ToLower()) should do the trick – NotAnAuthor Mar 19 '18 at 21:57
2

You can use .ToTitleCase()

var myString = "Project1:toyota:Corolla";
TextInfo textInfo = new CultureInfo( "en-US" ).TextInfo;
myString = textInfo.ToTitleCase(myString);
ikaikastine
  • 601
  • 8
  • 22
0

Regular expression alternative:

var result = Regex.Replace("Project1:toyota:Corolla", @"\b[a-z]", m => m.Value.ToUpper());
Slai
  • 22,144
  • 5
  • 45
  • 53