如果子字符串大于 x,如何替换 StringBuilder 中的子字符串?

How to replace a substring within a StringBuilder if the substring is greater than x?

我有一个 stringbuilder(sb),它是 sb.Appending 一长串 HTML(来自 CKEditor 文本框)。

我的问题是 CSS !important 标签不适用于在 CKEditor 中创建的 table。

我正在尝试为 table(如果已创建)设置最大宽度,并且仅当 table 的宽度值超过一定数量时才对其进行编辑。 IE width:900px 超过 200px,因此在将其插入 stringbuilder 之前用 width:200px 值替换该值 示例:

<style>
.div table{width:200px; !Important}
</style>

<p>"Lorem ipsum dolor sit amet, consectetur adipiscing 
    elit, sed do eiusmod 
    tempor incididunt ut labore et dolore magna aliqua."</p>

    <p>de Finibus Bonorum et Malorum", written by Cicero in 45 BC</p>

        <p>"Sed ut perspiciatis unde omnis iste natus error sit voluptatem 
            accusantium doloremque laudantium, totam"</p>

        <p> </p>

        <table border="1" cellpadding="1" cellspacing="1" style="height:55px; width:900px">
            <tbody>
                <tr>
                    <td> </td>
                    <td>Value</td>
                </tr>
                <tr>
                    <td>Example</td>
                    <td>50</td>
                </tr>
            </tbody>
        </table>

        <p> </p>

        <p>1914 translation by H. Rackham</p>

        <p>"But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system."</p>

我也尝试过一些 Regex 方法,但因为我不太熟悉它,所以我不想使用 Regex。

s.some_long_string就是上面问题中html的字符串

string TempString = s.some_long_string;    
string replace;

这将找到以 "width:" 开头、具有任意数量 and/or 个字符序列,然后以 "px" 结尾的字符串部分。

MatchCollection matchlist = Regex.Matches(s.some_long_string, "width:(.*)px");

这会收集 "width:" 弹出的所有实例。

var list = matchlist.Cast<Match>().Select(match => match.Value).ToList();

foreach (var v in list)
{
    replace = v;

这会将数字与字符串的其余部分分开

    var resultString = Regex.Match(v, @"\d+").Value;

    if (Convert.ToInt32(resultString) > 745)
    {
        TempString = TempString.Replace(replace, "width:745px");
        s.some_long_string = TempString;
    }
}