locked
How do I find all start and end indexes of a word from a paragraph RRS feed

  • Question

  • User722039657 posted

    Example -

    string paragraph = "Is this the last line or is there more? IS this part of the information I’m looking for ?";
    string serchWord = "is";

    The word "is" is exists 3 times in the paragraph (i.e) Is, is and IS.

    I want to get all start and end indexes of "is" with out case. (Note -  "this" as well contains "is" but it should not be considered because here "is" is not a word)

    Sunday, May 17, 2020 9:06 AM

Answers

  • User303363814 posted
    foreach (Match match in Regex.Matches(paragraph, $"\\b{serchWord}\\b", RegexOptions.IgnoreCase))
    {
       Console.WriteLine($"First character at {match.Index} last at {match.index + match.Length - 1}");
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, May 17, 2020 11:48 PM

All replies

  • User303363814 posted
    foreach (Match match in Regex.Matches(paragraph, $"\\b{serchWord}\\b", RegexOptions.IgnoreCase))
    {
       Console.WriteLine($"First character at {match.Index} last at {match.index + match.Length - 1}");
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, May 17, 2020 11:48 PM
  • User-1955300613 posted

    Hi,Senthil S R

    Thank you for your question!

    Here is my demo,you can refer to it:

    _________________________________________________________________________________

    using System;
    using System.Linq;

    class Program
    {
    static void Main(string[] args)
    {
    string s = "Is this the last line or is there more? IS this part of the information I’m looking for ?";
    string b = "is";
    var number = s.Trim('.').Split(" ").Where(a => a.ToUpper() == b.ToUpper()).Count();
    Console.WriteLine(number);
    }
    }

    __________________________________________________________________________________

    I hope this can help you.

    Best Regards

    Tsaisoul

    Thursday, May 21, 2020 2:29 AM
  • User722039657 posted

    Hi Tsaisoul,

    This is yielding the search count but that as well not satisfying if followed by any special characters like comma, full stop, exclamation, ... etc

    Sunday, May 31, 2020 9:23 AM