TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# First Sentence

This C# example program gets the first sentence from a paragraph. It uses a for-loop and switch statement.

First sentence. A first sentence has importance.

How can we get the first sentence in a paragraph? Parsing the English language is exceedingly hard. Instead of aiming for a perfect implementation, we present here a simple method.

Example. The FirstSentence method presented here loops through the paragraph string beginning at the first character. When the period is encountered, it checks that the period is followed by a whitespace. This signals the end of a sentence.

Also: In the switch, the "?" and the "!" characters are considered sentence ending characters.

C# program that gets first sentence

using System;

class Program
{
    static void Main()
    {
	string paragraph =
	    "Thank you for visiting this article. There are " +
	    "more articles on this website.";
	Console.WriteLine(FirstSentence(paragraph));

	paragraph =
	    "How can you make more money? The easiest thing to do " +
	    "would be to spend less.";
	Console.WriteLine(FirstSentence(paragraph));
    }

    public static string FirstSentence(string paragraph)
    {
	for (int i = 0; i < paragraph.Length; i++)
	{
	    switch (paragraph[i])
	    {
		case '.':
		    if (i < (paragraph.Length - 1) &&
			char.IsWhiteSpace(paragraph[i + 1]))
		    {
			goto case '!';
		    }
		    break;
		case '?':
		case '!':
		    return paragraph.Substring(0, i + 1);
	    }
	}
	return paragraph;
    }
}

Output

Thank you for visiting this article.
How can you make more money?

We require a period and space for the ending character of a sentence because many abbreviations are used in the English language. For example, a URL like "" has no sentence ending characters.

Note: This method has weaknesses. It will consider the title "Mr" to end a sentence. This problem occurs in other places too.

However: Creating a method that handles all cases in the English language is challenging.

Summary. Sometimes, a simple method that handles most cases is appropriate. This method will work for writing samples that exclude certain words. An interesting challenge in computer science is handling natural languages.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf