我正在使用以下代码。它仅在列表框中显示HTML文件 我想替换图1,图1.2,图2,图2.1等...... 图1 ...........在OutPut中
请帮帮我
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
FileInfo file = new FileInfo(textBox1.Text);
StreamReader stRead = file.OpenText();
while (!stRead.EndOfStream)
{
listBox1.Items.Add(stRead.ReadLine());
}
}
}
输出
<html>
<head>
</head>
<body>
<div>
<p class="epigraph"> very Figure 1 But thanks to you, we won't do it </p>
<p class="epigraph-right"> birthday Figure 1.1 Milton Friedman, November 8, 2002</p>
<p class="indent">Not Figure 2 able to take it any longer New York </p>
<p class="indent">Mr. Cutler Figure 2.1 of the parallel plunges</p>
</body>
</div>
</html>
答案 0 :(得分:1)
我认为你需要测试
FileInfo file = new FileInfo(textBox1.Text);
StreamReader stRead = file.OpenText();
while (!stRead.EndOfStream)
{
string strTemp = stRead.ReadLine();
for (int i = 1; i < MaxFigureCount; i++)
strTemp = strTemp.replace("Figure "+i+" ", "Figure "+i+".x ");
listBox1.Items.Add(strTemp);
}
你可以看到我不知道你的MaxFigureCount
我也".x"
因为我无法弄清楚你想要做什么。
我最后还有额外的空间,以确保我们不会错过2.1或1.2,如果它们已经存在。
答案 1 :(得分:0)
while (!stRead.EndOfStream)
{
string test = stRead.ReadLine();
string correctString = test.Replace("Figure 1", " Figure 1.2");
//Etc. etc.....
listBox1.Items.Add(correctString);
}
答案 2 :(得分:0)
尝试使用Regex.Replace:
listBox1.Items.Add(Regex.Replace(stRead.ReadLine(), @"\sFigure \d+.\d+\s", " Figure 1 "));
我做了一些测试,显然工作正常,但我不是正则表达式语法专家。