c# - Best way to do a string search and replace -
I have to write some code which highlights an HTML on specific keywords in the string.
If I have a list of strings with a comma and I would like to do one search and substitute for each entry in the list on the other string. What is the most effective way to do this?
I am currently doing a partition, then for a foreach and a Regex.Match example:
string wordTalkcheck = "this", "the" , "And"; String list array [] = wordpress check. Sample (','); String contentToReplace = "To work as a professional designer, is used for each other, but for some time he has to work in any way to work and work."; Forward (string work in list array) {if (match to regx match (content to replay, word + "\ \ s +", reggaeops.ec.ogor case) .full) {rijks rivne Revolted (content to replay, word + "\\ s +", string.format ("& lt; span style = \" background-color: yellow; \ "& gt; {0} ", Words), RegexOptions.IgnoreCase); }}
I'm not sure that this is the most effective way because the list of words can be long to check and the above code can be part of a loop to search and replace The bunch of stuff is
Do not do that if words can be modified by a user!
Your approach works perfectly without Reggaeus, just do a normal string. Replace.
If the input is secure, then you can also use a regex for all keywords, such as
return Regex. Replace (contentToReplace, "(| | | and |)", string.format ("& lt; span style = \" background-color: yellow; \ "> {0} ", words) RegexOptions.IgnoreCase);
Where "this | is | and" is just wordTok check
where commas with pipes are changed "."
BTW, instead of a comma-separated list you want, it will give you more flexibility to take a raggeon directly in the name of the list.
Comments
Post a Comment