In C #, it is difficult to Replace strings with case-insensitive characters. Even if it takes a lot of effort to Replace strings, the efficiency is still very low, the correct method should be to use a regular expression.
To use a regular expression, you must first reference the namespace:
Using System. Text. RegularExpressions;
Then, it is very simple to use:
Regex. Replace (string, substring to be replaced, Replace the substring character, RegexOptions. IgnoreCase)
The final parameter RegexOptions. IgnoreCase indicates that the case sensitivity is ignored.
However, I want to highlight all the matched substrings in a group of strings (that is, make the font style different from other parts of the string ), the following statement inserts an html tag at both ends of the string to highlight the string, but the highlighted string is the search string for the substring. The case is different from the original one.
For example, the keyString I searched in "13th Asp.net implementations" is "asp", and the replaced string is "13th asp.net implementations ", instead of "13th Asp.net implementations"
DocumentResume [I] = Regex. Replace (hitDoc. Get ("resume"), keyString, "" + keyString + "", RegexOptions. IgnoreCase );
Therefore, Replace directly using a regular expression cannot meet my needs. Instead, we need to use the Match search method of the regular expression (Match searches for a single entry and multiple Matchs entries ), insert the html tag before and after the matched substring. For details, refer to the following code:
String pain = hitDoc. Get ("resume"); // string
System. Text. RegularExpressions. MatchCollection m = Regex. Matches (pain, keyString, RegexOptions. IgnoreCase); // ignore keywords in case-insensitive search strings
For (int j = 0; j
{
// J × 31 adds the length of the pain string for inserting html tags. Note that the following two sentences cannot be exchanged. Otherwise, the HTML tag insertion error occurs.
Pain = pain. Insert (m [j]. Index + keyString. Length + j * 31), ""); // Insert the html Tag after the keyword
Pain = pain. Insert (m [j]. Index + j * 31), ""); // Insert the html tag before the keyword
}
Of course, html tags do not work if they are inserted randomly. Add the following custom style to the header area of the page code for displaying the inserted string.
<STYLE type = text/css>
<! --. Highlight {
Color: #00 FFFF;
Font-style: italic;
Font-size: larger;
} -->
</STYLE>
Nutian.