How to break string to sentences in C#

1 Answer

0 votes
using System;
using System.Collections.Generic;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            String s = "C# is a multi-paradigm programming language. " +
                       "Strong typing, functional, " +
                       "component-oriented, generic, object-oriented. " +
                       "C# is a general-purpose, " +
                       "object-oriented programming language.";
            var sentences = new List<String>();
            int pos = 0, start = 0;
            do
            {
                pos = s.IndexOf('.', start);
                if (pos >= 0) {
                    sentences.Add(s.Substring(start, pos - start + 1).Trim());
                    start = pos + 1;
                }
            } while (pos > 0);

            foreach (var sentence in sentences)
                Console.WriteLine(sentence);
        }
   }
}


/*
run:

C# is a multi-paradigm programming language.
Strong typing, functional, component-oriented, generic, object-oriented.
C# is a general-purpose, object-oriented programming language.

*/

 



answered Jul 26, 2018 by avibootz
...