Show number of stars instead of the number

淺唱寂寞╮ 提交于 2019-12-24 19:50:58

问题


I am making i site where people can make reviews of movies and when they make the review they need to put a rating to a movie, in a number of 1 to 5.

But when it print out on another page I want to print out stars insted of the number 3 for exemple.

For now I got this code:

StringBuilder movielistSB = new StringBuilder();
            foreach (var moviereview in movielistSB)
            {
                filmanmeldelserlisteSB.AppendFormat(
                    "<div id='moviereview'><artitcle><h3>{0}</h3>" + 
                    "<table><tr><td>ID:</td><td>{1}</td></tr>" + 
                    "<tr><td>Anmeldelse:</td><td>{2}</td></tr>" + 
                    "<tr><td>Rating:</td><td>{3}</td></tr></table></article></div>",
                    (string)moviereview.Element("title"),
                    (string)moviereview.Element("id"),
                    (string)moviereview.Element("review"),
                    (string)moviereview.Element("rating")
                );

And for the (string)moviereview.Element("rating") I want to print out for exempel 3 stars when a user has given the movie a rating of 3.

I tried to run a for loop, but I could not figur out how to get that to work. And I've tried to Google for a solution, but anyhow I can't get it to work.

Any ideas?


EDIT: For now I got this:

String star = @"<img src=""../images/star.png""/>";

    if (moviereviewlist.Count() > 0)
    {
        StringBuilder moviereviewlistSB = new StringBuilder();
        foreach (var moviereview in moviereviewlist)
        {
            filmanmeldelserlisteSB.AppendFormat(
                "<div id='moviereview'><artitcle><h3>{0}</h3>" + 
                "<table><tr><td>ID:</td><td>{1}</td></tr>" + 
                "<tr><td>Review:</td><td>{2}</td></tr>" + 
                "<tr><td>Rating:</td><td>{3}</td></tr></table></article></div>",
                (string)moviereview.Element("tittel"),
                (string)moviereview.Element("id"),
                (string)moviereview.Element("anmeldelse"),
                star.RepeatString((int)moviereview.Element("rating"))
            );

and this

    public static class StringExtensions
{
    public static string RepeatString(this string input, int count)
    {
        if (!string.IsNullOrEmpty(input))
        {
            return string.Empty;
        }

        StringBuilder builder = new StringBuilder(input.Length * count);
        for(int i = 0; i < count; ++i)
        {
            builder.Append(input);
        }
        return builder.ToString();
    }
}

I had to parse the `(int)filmanmeldelse.Element("rating")' to an int. But for somehow I can't get a image on the site. Its only blank and I have tried different ways, but It will not get a value to the site.


回答1:


Try this:

StringBuilder movielistSB = new StringBuilder();
foreach (var moviereview in movielistSB)
{
    filmanmeldelserlisteSB.AppendFormat(
            "<div id='moviereview'><artitcle><h3>{0}</h3>" + 
            "<table><tr><td>ID:</td><td>{1}</td></tr>" + 
            "<tr><td>Anmeldelse:</td><td>{2}</td></tr>" + 
            "<tr><td>Rating:</td><td>{3}</td></tr></table></article></div>",
            (string)moviereview.Element("title"),
            (string)moviereview.Element("id"),
            (string)moviereview.Element("review"),
            new String('*', moviereview.Element("rating"))
        );

This works, because String has a constructor that takes a Character to repeat, and a number of times to repeat it.

So the code:

new String('X', 5);

would result in the string "XXXXX" (five X's in a row)


EDIT Poster asks if something similar can be used to insert Image-Links.

Reply: No. This String method repeats a single character, not a block of text.
If you want to repeat a block of text, you'll want something like:

String starImage = @"<img src=""StarImage.jpg""/>";

filmanmeldelserlisteSB.AppendFormat(
            "<div id='moviereview'><artitcle><h3>{0}</h3>" + 
            "<table><tr><td>ID:</td><td>{1}</td></tr>" + 
            "<tr><td>Anmeldelse:</td><td>{2}</td></tr>" + 
            "<tr><td>Rating:</td><td>{3}</td></tr></table></article></div>",
            (string)moviereview.Element("title"),
            (string)moviereview.Element("id"),
            (string)moviereview.Element("review"),
            starImage.RepeatString(movieReview.Element("rating"))
        );

But you'll have to write your own RepeatString method.
A guide to doing that can be found on this question


EDIT #2

Writing an extension method is not hard.
I would put this class somewhere in your project's namespace.

public static class StringExtensions
{
    public static string RepeatString(this string input, int count)
    {
        if (string.IsNullOrEmpty(input))
        {
            return string.Empty;
        }

        StringBuilder builder = new StringBuilder(input.Length * count);
        for(int i = 0; i < count; ++i)
        {
            builder.Append(input);
        }
        return builder.ToString();
    }
}

Geesh... you really can't debug, can you?

You have an extra ! in this code:

public static string RepeatString(this string input, int count)
{
    if (!string.IsNullOrEmpty(input))  // <-- Extra !
    {
        return string.Empty;
    }

Its a pretty simple statement!
"If the string is Null or Empty, return an Empty string"

But you have it backwards, so it effectively says:
"If the string has text in it, return an Empty string".

That makes no sense.




回答2:


Instead of:

(string)moviereview.Element("rating")

Use:

new string('*', int.Parse((string)moviereview.Element("rating")))



回答3:


I actually got this code to work:

public static string Multiply(this string source, int multiplier)
    {
        StringBuilder sb = new StringBuilder(multiplier * source.Length);
        for (int i = 0; i < multiplier; i++)
        {
            sb.Append(source);
        }

        return sb.ToString();
    }

Thanks for the help guys!



来源:https://stackoverflow.com/questions/22617511/show-number-of-stars-instead-of-the-number

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!