Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor String Replace

I was looking for a way to do some kind of string replace.

Found this snippet on here, but it gives me the error that MvcHtmlString is not defined:

@MvcHtmlString.Create(Html.Encode(comic.name).Replace(" ", "-"));

Whole Section Of Code:

@{
    var comicName = UrlData[0];

    var db = Database.Open("PhotoGallery");
    var comics = db.Query(@"SELECT * FROM Comics WHERE name = @0", comicName).ToList();

    Page.Title = comicName;
}
<div><a href="@Href("~/")">Home</a> -> <a href="@Href("~/Comics")">Comics Categories</a> -> <strong>@comicName</strong></div>   
<div style="clear: both;">&nbsp;</div>
<div class="sidebar">
<center><img src="@Href("~/Images/Comics", comicName + ".jpg")" title="@comicName" width="320" height="498" /><br />
@comics.Count Issue(s)</center>
</div>
<div class="main">
    <h1>@comicName</h1>
@foreach (var comic in comics) { 
    @MvcHtmlString.Create(Html.Encode(comic.name).Replace(" ", "-"));
<ul class="thumbnails gallery">
        <li class="gallery">
            <a href="@Href("~/Comic/View", comic.name + " " + comic.issue)">
                <img title="@comic.name #@comic.issue" src="@Href("~/Images/Comics", comicName + "/" + comic.issue + ".jpg")" class="thumbnail-no-border" width="200" height="200" />
                <span class="below-image">@comic.name #@comic.issue</span>
                <span class="image-overlay"><strong>@comic.pages Pages</strong></span>
            </a>
        </li>
</ul>    
}    
</div>
like image 908
rackemup420 Avatar asked Jul 02 '11 17:07

rackemup420


People also ask

How to Replace string in. net?

Replace(String, String) Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string.

How do I replace a character in a string?

The Java string replace() method will replace a character or substring with another character or string. The syntax for the replace() method is string_name. replace(old_string, new_string) with old_string being the substring you'd like to replace and new_string being the substring that will take its place.

How do you replace a string in Java?

Java String replace() MethodThe replace() method searches a string for a specified character, and returns a new string where the specified character(s) are replaced.


1 Answers

In MVC 3 output is encoded by default, so this should be enough:

@comic.name.Replace(" ", "-")
like image 76
Sergi Papaseit Avatar answered Sep 23 '22 00:09

Sergi Papaseit