【问题标题】:Insert new line every 4 items in a string?在字符串中每 4 个项目插入新行?
【发布时间】:2014-06-19 14:45:49
【问题描述】:

我有一个字符串,它一个接一个地设置缩略图,没有换行符。图像一旦达到它们所在区域的最大宽度,就会自动换行。我想在每 4 个缩略图之后插入一个换行符,我的代码几乎可以工作,但它会在第 1 个之后不断添加新行图片——这看起来不对。对于 7 个缩略图,应该只添加一个换行符,并且应该在第 4 个图像之后。

以下是用于调用并使用 SQL 数据库中的缩略图信息构建字符串的类信息:

protected List<GlassItem> GetGlassItems(Guid CurrentPage)
    {
        var items = new List<GlassItem>();
        using (MySqlConnection cn = new MySqlConnection(ConfigurationManager.ConnectionStrings["Sitefinity"].ToString()))
        {
            cn.Open();

            MySqlCommand cmd = new MySqlCommand("SELECT id, BrandID, GlassName, Thumbnail, LargeImage, Ordinal, (SELECT Window_Brands.BrandName FROM Window_Brands WHERE Window_Brands.BrandPage = BrandID) AS BrandName FROM Window_Brand_cutglass WHERE BrandID = ?PageID AND Thumbnail IS NOT NULL AND LargeImage IS NOT NULL ORDER BY Ordinal", cn);
            cmd.Parameters.Add(new MySqlParameter("PageID", CurrentPage.ToString()));

            using (MySqlDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    if (reader["Thumbnail"].ToString().Length > 1 && reader["LargeImage"].ToString().Length > 1)
                    {
                        items.Add(new GlassItem
                        {
                            LargeImagePath = Revere.GetImagePath(reader["LargeImage"].ToString()),
                            ThumbnailPath = Revere.GetImagePath(reader["Thumbnail"].ToString()),
                            GlassName = reader["GlassName"].ToString()
                        });
                    }
                }
            }
            cn.Close();
        }
        return items;
    }

这是使用 GetGlassItems 类的代码。这是在每 4 项之后插入一个新行的地方,但也在第一项之后添加一个新行:

protected String BuildCutGlass(Guid CurrentPage)
 {
        var items = GetGlassItems(new Guid(CurrentPage.ToString()));
        StringBuilder Glass = new StringBuilder();

        for (int i = 0; i < items.Count; i++)
        {
            Glass.Append(String.Format("<div class='GlassItem'><a href='{0}' class='CutGlassPopup Icon'><img src='{1}' alt='{2}' width='77' height='77' /></a><a href='{0}' class='CutGlassPopup'>{2}</a></div>",
                                        items[i].LargeImagePath,
                                        items[i].ThumbnailPath,
                                        items[i].GlassName));
            Console.Write("count: " + items.Count.ToString() + "<br />");
            if (i == 4)
            {
                // new line every 4 items
                Glass.Append("<div style='clear: both;'></div>");
            }
        }

        return Glass.ToString();
    }

如何编辑上述代码,以便在第一个缩略图之后不再添加新行?

【问题讨论】:

    标签: c# sql count newline stringbuilder


    【解决方案1】:

    您需要通过modulus operator 进行检查。

    if (i % 4 == 0)
    

    这将让您每四行插入一个新行。

    但这也会为第一项插入一个新行,因为i 将是00 % 4 将返回0。因此,将您的支票修改为:

    if(i != 0 && i % 4 == 0)
    

    您当前的支票i == 4 只会在前四行之后添加额外的一行。

    【讨论】:

    • 我尝试了模数检查,但我遇到了同样的问题:在第一个缩略图之后添加一个新行,然后在每第四个缩略图之后添加一个新行。我不知道为什么它必须在第一项之后继续换行,但这不是我想要发生的。
    • @SaraDeJaneiro 0 % 0 == 0。在 if 语句中设置排除项。
    • @Habib 不应该是 (i-1) % 4 == 0 吗?您的示例在第 0 行和第 4 行之后打印一个。我们想要在索引三之后。
    • @Habib 这对我来说很完美。非常感谢!
    • @Habib 是的对不起 i + 1 不是 i - 1。不知道我在想什么。
    猜你喜欢
    • 2017-03-25
    • 2019-06-11
    • 1970-01-01
    • 2019-09-22
    • 2017-05-09
    • 2013-06-16
    • 2011-11-29
    • 1970-01-01
    • 2011-03-16
    相关资源
    最近更新 更多