Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8698343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:42:35+00:00 2026-06-13T01:42:35+00:00

I was trying to find a certain part of the text having a specific

  • 0

I was trying to find a certain part of the text having a specific keyword. I did it but I am thinking there might be a better way to do it.

Here is my code,

    /// <summary>
    /// Return String containing the search word
    /// </summary>
    /// <param name="strInput">Raw Input</param>
    /// <param name="searchWord">Search Word</param>
    /// <param name="minLength">Output Text Length</param>
    /// <returns></returns>
    public static String StringHavingSearchPattern(String strInput, String searchWord, Int32 minLength)
    {
        //considering the return string empty
        String rtn = String.Empty;
        //
        if (String.IsNullOrEmpty(strInput))
        {
            return rtn;
        }
        // consider the raw input as return;
        //rtn = strInput;
        int length = strInput.Length;

        //if the rawinput length is greater||equal than/to the min length
        if (length >= minLength)
        {
            int currentSearchIndex = -1;
            searchWord = String.Format(@"{0}", searchWord);
            //currentSearchIndex = strInput.IndexOf(searchWord,StringComparison.InvariantCultureIgnoreCase);
            Match m = Regex.Match(strInput, searchWord, RegexOptions.Multiline | RegexOptions.IgnoreCase);
            if (m.Success)
            {
                currentSearchIndex = m.Index;
                if (currentSearchIndex >= 0)
                {
                    if (currentSearchIndex > 9)
                    {
                        if ((length - currentSearchIndex - 1) >= minLength)
                        {
                            rtn = strInput.Substring((currentSearchIndex - 9), minLength);
                        }
                        else
                        {
                            rtn = strInput.Substring((currentSearchIndex - 9), length - currentSearchIndex - 1);
                        }
                    }
                    else
                    {
                        rtn = strInput.Substring(0, minLength);
                    }
                }
                else
                {
                    rtn = strInput.Substring(0, minLength);
                }
            }
        }
        rtn = Regex.Replace(rtn, searchWord, String.Format("<span style='color:yellow;background-color:blue;'>{0}</span>", searchWord), RegexOptions.IgnoreCase | RegexOptions.Multiline);

        //rtn = rtn.Replace(searchWord, String.Format("<span style='color:yellow;background-color:blue;'>{0}</span>", searchWord));
        return rtn;
    }

Looking for your kind suggestions to improve it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:42:37+00:00Added an answer on June 13, 2026 at 1:42 am

    Your hunch was right; you can use a single, simple regular expression to achieve this:

    Match m = Regex.Match(strInput, "(.{0," + minLength.ToString() + "}" + searchWord + ".*)", ...
    

    This will create, for example for searchWord “keyword” and minLength 10, the following regex,

    (.{0,10}keyword.*)
    

    which means, “Capture up to 10 characters followed by ‘keyword’ and the remainder of the text.” Then, you can grab the captured group:

    m.Groups[1].Value
    

    Note that the case where there are less than minLength characters before the search term is covered.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am really having trouble trying to figure out how to find certain records
I'm trying to find a way to maximize & minimize a certain application using
I'm trying to create a simple game, but I can't find a certain memory
I'm trying to find a solution to highlight part of a text file in
Trying to find a good way to read a text file and find the
I'm trying to find certain strings in a lot of files (cc. 2000 files).
i am trying to find a certain tag in a html-page with java. all
I have a lot of nested dictionaries, I am trying to find a certain
I have a web application project. I am trying to find out why certain
I'm trying to find out the time between certain fields in my tables. However

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.