Probably a very basic question, but I don't seem to find the solution myself...
I want to replace all occurences of a string by that same string, but enclosed in <span></span>. It is a bit more complicated, since the substring to be replaced is a parameter passed to my page in lower case. But the string to be replaced van be a combination
of lower and upper case.
To break it down in steps:
- Find all occurences of substring x, but ignore case -> let's call the result x'
- Replace the occurences of substring x', by <span>x'</span>
Any idea how to do this?
+ do I need to perform this in the controller class or in the code of the view?
The 2nd part of my question concerned the location of this piece of code. Should it go in the view or in the controller?
imho, in ASP.NET MVC, such code is one of the black boxes for the Model.
The View is only for rendering ... replacing all occurences in a string is
not rendering.
The Controller also should have minimal logic imho ... for myself, i like to think of the pattern as McV, with the
controller being the servant of the Model and the messenger between the
Model and the View.
one way to think about the controller is analogous to a video game ... the
controller is what moves things around ... the screen is the UI for the video game, and the Model for the video game is the logic part of the video game.
g.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
I would say where you do it depends on the context. If the text is being surrounded by <span> tags simply in order to be displayed nicely then this could go in the view as it is no different than taking any other data passed to the view and massaging it for
presentation.
The following are special characters for regular expressions [\^$.|?*+(). In order to search for them you would need to escape the special characters with a \. You can use \b to indicate a word boundary. For example you could use this command to only replace
the "is" on its own, not the "is" that is part of missing whilst ignoring upper/lower case; Regex.Replace("Text is Missing", "\bis\b", "is not", RegexOptions.IgnoreCase), and you could use this to replace "mi$take" in your string Regex.Replace(stringToCheck,
"mi\$take", "mistake", RegexOptions.IgnoreCase). See this website for further information http://www.regular-expressions.info/reference.html
MCSD, MCPD, MCTS
Marked as answer by Allen Li - MSFT on May 08, 2012 01:35 AM
Ron83
0 Points
7 Posts
Replace all occurences in a string
May 02, 2012 07:16 PM|LINK
Hi all,
Probably a very basic question, but I don't seem to find the solution myself...
I want to replace all occurences of a string by that same string, but enclosed in <span></span>. It is a bit more complicated, since the substring to be replaced is a parameter passed to my page in lower case. But the string to be replaced van be a combination of lower and upper case.
To break it down in steps:
- Find all occurences of substring x, but ignore case -> let's call the result x'
- Replace the occurences of substring x', by <span>x'</span>
Any idea how to do this?
+ do I need to perform this in the controller class or in the code of the view?
frez
Contributor
5418 Points
913 Posts
Re: Replace all occurences in a string
May 02, 2012 07:57 PM|LINK
Lookup RegEx.Replace using the RegExOption IgnoreCase.
Ron83
0 Points
7 Posts
Re: Replace all occurences in a string
May 02, 2012 10:00 PM|LINK
Thanks for this pointer. I will look into it.
The 2nd part of my question concerned the location of this piece of code. Should it go in the view or in the controller?
gerrylowry
All-Star
20577 Points
5721 Posts
Re: Replace all occurences in a string
May 03, 2012 09:13 AM|LINK
@ Ron83
imho, in ASP.NET MVC, such code is one of the black boxes for the Model.
The View is only for rendering ... replacing all occurences in a string is not rendering.
The Controller also should have minimal logic imho ... for myself, i like to think of the pattern as McV, with the controller being the servant of the Model and the messenger between the Model and the View.
one way to think about the controller is analogous to a video game ... the controller is what moves things around ... the screen is the UI for the video game, and the Model for the video game is the logic part of the video game.
g.
frez
Contributor
5418 Points
913 Posts
Re: Replace all occurences in a string
May 03, 2012 09:24 AM|LINK
Rohit Binjol...
Member
84 Points
33 Posts
Re: Replace all occurences in a string
May 03, 2012 12:44 PM|LINK
Hi,
You can do it using string builder class like::
StringBuilder str = new StringBuilder("rexpex");
str.Replace("x", "<span>x<span>");
return str.ToString();
Hope this helps.......
Ron83
0 Points
7 Posts
Re: Replace all occurences in a string
May 03, 2012 04:01 PM|LINK
Thanks for your great help so far.
The additional complexity here is that I want to replace x by <span>x</span>, but also X by <span>X</X>
So I now build a MatchCollection to store all found strings that are equal to x, ignoring case.
MatchCollection mc = Regex.Matches(text, @word, RegexOptions.IgnoreCase);
Then I loop through the MatchCollection and do a replace.
2 issues remain:
- currently, my algorithm also detects partials: if I look for 'the', it will also detect the 'the' in the word 'other'
- my regex crashes when the lookup word contains characters like ), $, ...
How do I solve these problems?
frez
Contributor
5418 Points
913 Posts
Re: Replace all occurences in a string
May 04, 2012 08:23 AM|LINK