views:

500

answers:

5

I know how to capitalize first letter in each word. But I want to know how to capitalize first letter of each sentence in C#.

+3  A: 

As a first approximation, you could probably treat any sequence like [a-z]\.[ \n\t] as the end of a sentence.

Jerry Coffin
[.!?]\s(?[a-z])
Joel Coehoorn
@Joel:good suggestion -- a definite improvement.
Jerry Coffin
@Joel Perhaps capturing the text before the punctuation too? It could help filtering out common abbreviations like Dr., Ms., Mrs., etc.
statenjason
+1  A: 

There's some VB code on this page which shouldn't be too hard to convert to C#.

However, subsequent posts point out the errors in the algorithm.

This blog has some C# code which claims to work:

It auto capitalises the first letter after every full stop (period), question mark and exclamation mark.

UPDATE 16 Feb 2010: I’ve reworked it so that it doesn’t affect strings such as URL’s and the like

ChrisF
+10  A: 

This is not necessarily a trivial problem. Sentences can end with a number of different punctuation marks, and those same punctuation marks don't always denote the end of a sentence (abbreviations like Dr. may pose a particular problem because there are potentially many of them).

That being said, you might be able to get a "good enough" solution by using regular expressions to look for words after a sentence-ending punctuation, but you would have to add quite a few special cases. It might be easier to process the string character by character or word by word. You would still have to handle all the same special cases, but it might be easier than trying to build that into a regex.

There are lots of weird rules for grammar and punctuation. Any solution you come up with probably won't be able to take them all into account. Some things to consider:

  • Sentences can end with different punctuation marks (. ! ?)
  • Some punctuation marks that end sentences might also be used in the middle of a sentence (e.g. abbreviations such as Dr. Mr. e.g.)
  • Sentences could contain nested sentences. Quotations could pose a particular problem (e.g. He said, "This is a hard problem! I wonder," he mused, "if it can be solved.")
CodeSavvyGeek
How'd you get so many upvotes without actually providing an answer for the question? I want to know the name of your cologne!
John K
@jdk: Fair point, but the spirit of my answer was more that you probably can't do a really good job of this in all cases. I added a little more advice for creating a "good enough" solution that might work *most* of the time.
CodeSavvyGeek
When in grad school you need to either find a good solution or prove that it is NP-complete.This answer is like proving it is NP-complete (aka just give up).
earlNameless
@earlNameless: Even for NP-complete problems, there are sometimes heuristics that can give good approximations (e.g. traveling salesman: http://en.wikipedia.org/wiki/Travelling_salesman_problem). You can't find a really good solution, but you might be able to find a "good enough" approximation.
CodeSavvyGeek
A: 

Don't forget sentences with parentheses. Also, * if used as an idicator for bold text.

http://www.grammarbook.com/punctuation/parens.asp

M. Hagen
A: 

Hi,

I find this after my trials

http://www.mardymonkey.co.uk/blog/index.php/2010/01/auto-capitalise-a-text-control-in-wpf/

it works well

enrique