Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,851 questions

51,772 answers

573 users

How to extract sentences with specific words of from text using C#

1 Answer

0 votes
using System;
using System.Linq; 

class Program
{
    static int countOccurences(string text, string searchTerm) { 
        char[] separators = {'.', '?', '!', ' ', ';', ':', ',', '-'};
        
        string[] arr = text.Split(' '); 
        
        var queryResult = from word in arr  
                         where word.Equals(searchTerm, StringComparison.InvariantCultureIgnoreCase)  
                         select word;  
   
        return queryResult.Count();  
    } 
    
    static void Main() {
        string text = @"
The C# programming language was designed by Anders Hejlsberg from Microsoft in 2000 and was later 
approved as an international standard by Ecma (ECMA-334) in 2002 and ISO/IEC (ISO/IEC 23270 
and 20619[c]) in 2003. Microsoft introduced C# along with .NET Framework and Visual Studio, 
both of which were closed-source. At the time, Microsoft had no open-source products. 
Four years later, in 2004, a free and open-source project called Mono began, providing 
a cross-platform compiler and runtime environment for the C# programming language. 
A decade later, Microsoft released Visual Studio Code (code editor), Roslyn (compiler), 
and the unified .NET platform (software framework), all of which support C# and are free, 
open-source, and cross-platform. Mono also joined Microsoft but was not merged into .NET. 
";
        char[] sentenceSeparators = {'.', '?', '!'};
        string[] sentences = text.Split(sentenceSeparators,  StringSplitOptions.RemoveEmptyEntries);

        string[] wordsToMatchInSentences = {"programming", "language"};

        char[] separators = {'.', '?', '!', ' ', ';', ':', ','};
        
        var queryResult = from sentence in sentences
                    let w = sentence.Split(separators,StringSplitOptions.RemoveEmptyEntries)
                    where w.Distinct().Intersect(wordsToMatchInSentences).Count() == wordsToMatchInSentences.Count()
                    select sentence;

        foreach (string str in queryResult) {
            Console.WriteLine(str);
        }
    }
}
 
 
 
 
/*
run:
 
The C# programming language was designed by Anders Hejlsberg from Microsoft in 2000 and was later 
approved as an international standard by Ecma (ECMA-334) in 2002 and ISO/IEC (ISO/IEC 23270 
and 20619[c]) in 2003
 
Four years later, in 2004, a free and open-source project called Mono began, providing 
a cross-platform compiler and runtime environment for the C# programming language

*/

 



answered May 1, 2024 by avibootz
...