-2

Sentence capitalization:

private string SentenceCapitalizer(string input)
{
 char delim = '.';
 string letter1;
 string[] tokens = input.Split(delim);
 foreach (string phrase in tokens)
 {
   letter1 = phrase.Remove(0);
   letter1.ToUpper();
 }
 return input;
}

Please keep in mind that this is only one of the methods.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 3
    Possible duplicate of [Make first letter of a string upper case (for maximum performance)](http://stackoverflow.com/questions/4135317/make-first-letter-of-a-string-upper-case-for-maximum-performance) – Mark Mar 24 '17 at 03:45

1 Answers1

1

First, take a look at the signature of ToUpper() and notice it returns a string. This doesn't modify the string you call it on; rather it returns a new string result from that operation.

In your case you have the phrases already. You can take the first character of a phrase with phrase[0] or phrase.First(). You should also take a look at Substring which gives you a range of characters from a string.

Putting that all together you could do something like:

phrase = phrase[0].ToString().ToUpper() + phrase.Substring(1);

What this does is take the first character from phrase and turn it from a char to a string which is what you need to call ToUpper() which you then concatenate with the remainder of the phrase using Substring starting at position 1 (which is the second character) and assign it back to phrase.

Erik Noren
  • 4,279
  • 1
  • 23
  • 29
  • 1
    I would stick with using `Substring` - as in `phrase = phrase.Substring(0, 1).ToUpper() + phrase.Substring(1);` – Enigmativity Mar 24 '17 at 05:41
  • 1
    That's good too and to some it might be more obvious the full string is being accounted for - more self-documenting. There's a dozen ways of accomplishing the task and a lot comes down to style. I was hoping to avoid answering the question of "best way" to do it and instead aimed for explaining the behavior so it's clear why the code does what it does and how to find out more independently. – Erik Noren Mar 24 '17 at 05:46