简体   繁体   English

在MVC控制器中,相同的string.replace()具有两种不同的含义

[英]same string.replace() given two different meanings in MVC controller

I'm trying to create something like the bool system stack overflow have in mvc controller. 我正在尝试在mvc控制器中创建类似bool系统堆栈溢出的内容。 The code under is working perfect if i change the last or first dobbel star to something else like /*, but what if i want both to be the same? 如果我将最后一个或第一个多贝贝星更改为/ *,则下面的代码工作完美,但是如果我希望两个都相同怎么办? like first time ** will be < b> and next time ** will be < /b>. 例如第一次**是<b>,而下一次**是</ b>。

[HttpPost]
[ValidateInput(false)]
public ActionResult Comment(Models.CommentModel s)
    {

        StringBuilder sbComments = new StringBuilder();
        sbComments.Append(HttpUtility.HtmlEncode(s.comment));

        sbComments.Replace("**", "&lt;b&gt;");
        sbComments.Replace("**", "&lt;/b&gt;");
        sbComments.Replace("&lt;b&gt;", "<b>");
        sbComments.Replace("&lt;/b&gt;", "</b>");

        s.comment = sbComments.ToString();

        var db = new WebApplication1.Models.ApplicationDbContext();

        if (ModelState.IsValid)
        {  
            db.Comments.Add(s);
            db.SaveChanges();
            return RedirectToAction("Comment");
        }
        return View(s);
    }

My solution thanks to baiyangcao`s answer: 我的解决方案感谢baiyangcao的回答:

Regular expression might look complex, but it's not that hard to understand. 正则表达式可能看起来很复杂,但并不难理解。 This site http://regexr.com/ helpet me alot to understand this. 这个站点http://regexr.com/帮助我了解了很多。

public ActionResult Comment(Models.CommentModel s)
    {
        Regex fat = new Regex(@"\*\*(.*?)\*\*");
        Regex italic = new Regex(@"_(.*?)_");
        Regex largeText = new Regex(@"#(.*?)#");

        s.kommentar = HttpUtility.HtmlEncode(s.comment);

        s.comment = largeText.Replace(s.comment, "<h1>$1</h1>");
        s.comment = fat.Replace(s.comment, "<b>$1</b>");
        s.comment = italic.Replace(s.comment, "<i>$1</i>");

        //this is the database I am adding my comments to
        var db = new WebApplication1.Models.ApplicationDbContext();

        if (ModelState.IsValid)
        {    
            db.Comments.Add(s);
            db.SaveChanges();
            return RedirectToAction("Comment");
        }
        return View(s);
    }

can't find Regex() ? 找不到Regex()吗? Remember to add using System.Text.RegularExpressions; 记住要using System.Text.RegularExpressions;添加using System.Text.RegularExpressions; library on top of your page. 库位于页面顶部。

You can use this extension: 您可以使用以下扩展名:

public static string replaceHipHip(this string text, string old, string hip, string hop)
{
    var result = new StringBuilder();
    bool b = true;
    int i = 0;
    while(i>=0)
    {
        int j = text.IndexOf(old, i);
        if (j == -1)
        {
            result.Append(text.Substring(i));
            break;
        }
        else
        {
            result.Append(text.Substring(i, j - i));
            if (b)
                result.Append(hip);
            else
                result.Append(hop);

            b ^= true;
            i = j+old.Length;
        }
    }
    return result.ToString();
}

Then your can write: 然后,您可以编写:

string text = "Hello **this** is a **dummy** text";
Console.WriteLine(text.replaceHipHip("**", "<b>", "</b>"));

Which outputs: 哪个输出:

Hello this is a dummy text 您好, 是一个虚拟文本

You can try use regular expression to do the replacement, like this: 您可以尝试使用正则表达式进行替换,如下所示:

Regex regexb = new Regex("\*\*(.*?)\*\*");
string comment = regexb.Replace(HttpUtility.HtmlEncode(s.comment), "<b>$1</b>");

Hope this can help 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM