让我们说,给出下面的html字符串:
<b>The quick <span>bro</span>wn fox jump over the lazy dog</b>
我现在的数据是:
并使最终结果成为:
<b>The quick <span>yellow</span> fox jump over the lazy dog</b>
基本上我能想到的就是将每个角色砍成一个列表, 然后使用flag来确定该字符是否为HTML开始/结束标记。如果没有,则用每个字符替换它。如果长度已经结束,则附加剩余的字符。如果原始文本比替换文本短,那么我们将删除剩余的字符,直到达到长度。
任何人都有更好的主意吗?
答案 0 :(得分:0)
读取该字符串,就像它是XML一样,将与每个节点相关的值添加到列表中,然后通过添加列表中每个单词的计数找到要替换的单词,直到达到起始索引(可以停止)即使在一个单词的中间,例如:&#34;&#34;,&#34; quick&#34;,&#34; brown&#34;有3个单词,让我们说你想要从第5位开始。第一个单词的计数小于5,所以你可以继续前进,看到第一个单词的计数+第二个单词的计数是8,大于5,所以你需要停在第二个单词。第二个单词中要替换的第一个字符是你的起始索引减去第二个单词之前的总计数,在这种情况下是5-3 = 2,所以第二个单词中的第二个字符是第一个被替换的。如果你的单词比你需要在下一个单词中替换的剩余字符长,直到你达到你的长度)。更换单词后,再次使用html标签构建字符串。
答案 1 :(得分:0)
以下是我写的单元测试: 它将使用“删除”然后“插入”方法。 所以基本上它可以涵盖一些情况,例如替换2个不同节点之间的文本。 并处理中文措辞或特殊字符。
有没有人有更好的方法?
[TestCase(@"<b>The quick <span>bro</span>wn fox jump over the lazy dog</b>", "<b>The quick <span>yellow</span> fox jump over the lazy dog</b>", 10, 5, "yellow", TestName = "Find_And_Replace 1")]
[TestCase(@"<b>The <span>quick</span> <span>bro</span>wn fox jump over the lazy dog</b>", "<b>The <span>quick</span> <span>yellow</span> fox jump over the lazy dog</b>", 10, 5, "yellow", TestName = "Find_And_Replace 2")]
[TestCase(@"<b>The quick brown & yellow tiger fox jump over the lazy dog</b>", @"<b>The quick brown & yellow croco fox jump over the lazy dog</b>", 25, 5, "croco", TestName = "Find_And_Replace 3")]
[TestCase(@"<div style=""text-align: left;""><span style=""font-size: 11pt; font-family: 'Calibri';"">The </span><span style=""font-size: 11pt; font-family: 'Calibri';"">快快的</span><span style=""font-size: 11pt; font-family: 'Calibri';""> brown fox jump over the lazy dog</span></div>", @"<div style=""text-align: left;""><span style=""font-size: 11pt; font-family: 'Calibri';"">The </span><span style=""font-size: 11pt; font-family: 'Calibri';"">快快的</span><span style=""font-size: 11pt; font-family: 'Calibri';""> brown fox bend under the lazy dog</span></div>", 18, 9, "bend under", TestName = "Find_And_Replace 4")]
[TestCase(@"<b>The quick <span>bro</span>wn fox jump over the lazy dog</b>", "<b>The quick <span>red</span> fox jump over the lazy dog</b>", 10, 5, "red", TestName = "Find_And_Replace 5")]
[TestCase(@"<div style = ""text-align: left;"" >< span style=""font-size: 11pt; font-family: 'Calibri';"">The quick</span><span style = ""font-size: 11pt; font-family: 'Calibri'; font-weight: bold;"" > brown & sl</span><span style=""font-size: 11pt; font-family: 'Calibri';"">ow tort</span><span style=""font-size: 11pt; font-family: 'Calibri'; text-decoration: underline;"" data-underline-style=""single"">oise jump</span><span style=""font-size: 11pt; font-family: 'Calibri';""> over the lazy dog</span></div>", @"<div style = ""text-align: left;"" >< span style=""font-size: 11pt; font-family: 'Calibri';"">The quick</span><span style = ""font-size: 11pt; font-family: 'Calibri'; font-weight: bold;"" > brown or fat pig</span><span style=""font-size: 11pt; font-family: 'Calibri';""></span><span style=""font-size: 11pt; font-family: 'Calibri'; text-decoration: underline;"" data-underline-style=""single""> jump</span><span style=""font-size: 11pt; font-family: 'Calibri';""> over the lazy dog</span></div>", 16, 15, "or fat pig", TestName = "Find_And_Replace 6")]
public void Find_And_Replace(string originalParagraphText, string expected, int startIndex, int length, string replaceText)
{
var insideHtmlTag = false;
var insideSpecialCharacter = false;
var currentIndex = 0;
var foundStartRawIndex = 0;
var currentRawIndex = 0;
var isFound = false;
var sb = new StringBuilder();
foreach (var character in originalParagraphText.ToCharArray())
{
var appendIntoNewHtml = true;
if (character == '<')
{
insideHtmlTag = true;
}
if (!insideHtmlTag)
{
if (currentIndex >= startIndex && currentIndex < (startIndex + length))
{
if (!isFound)
{
foundStartRawIndex = currentRawIndex;
isFound = true;
}
appendIntoNewHtml = false;
}
if (character == '&')
{
insideSpecialCharacter = true;
}
else if (insideSpecialCharacter && character == ';')
{
insideSpecialCharacter = false;
}
if (!insideSpecialCharacter)
{
currentIndex++;
}
}
if (character == '>')
{
insideHtmlTag = false;
}
if (appendIntoNewHtml)
{
sb.Append(character);
}
currentRawIndex++;
}
//Console.WriteLine(sb.ToString());
var actual = sb.Insert(foundStartRawIndex, replaceText).ToString();
//Console.WriteLine($@"Current Found Raw Index: {foundStartRawIndex}");
//Console.WriteLine($@"Replace Text : {actual}");
Assert.AreEqual(expected, actual);
}