Note that there are some explanatory texts on larger screens.

plurals
  1. POReturn part of the text using Regex and C#
    primarykey
    data
    text
    <p>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.</p> <p>Here is my code,</p> <pre><code> /// &lt;summary&gt; /// Return String containing the search word /// &lt;/summary&gt; /// &lt;param name="strInput"&gt;Raw Input&lt;/param&gt; /// &lt;param name="searchWord"&gt;Search Word&lt;/param&gt; /// &lt;param name="minLength"&gt;Output Text Length&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; 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 &gt;= 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 &gt;= 0) { if (currentSearchIndex &gt; 9) { if ((length - currentSearchIndex - 1) &gt;= 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("&lt;span style='color:yellow;background-color:blue;'&gt;{0}&lt;/span&gt;", searchWord), RegexOptions.IgnoreCase | RegexOptions.Multiline); //rtn = rtn.Replace(searchWord, String.Format("&lt;span style='color:yellow;background-color:blue;'&gt;{0}&lt;/span&gt;", searchWord)); return rtn; } </code></pre> <p>Looking for your kind suggestions to improve it.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload