本帖最后由 极品黑车 于 2022-11-15 14:56 编辑
拿了个测试的winform写了一下,结果放到一个全局变量List<string>里,测试用的数据是lz主帖里的,打断点能得到结果已经在ls里了
递归函数里写的可能繁琐了一点,用正则应该会更快
[C#] 纯文本查看 复制代码 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace winform_test
{
public partial class Form1 : Form
{
public List<string> ls;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
load();
}
private void load()
{
string a = "<div class=\"row row-gutter-20\">[size=6]<div class=\"col col-4 col-gutter-20\"><a href=\"/gif/4920k5k.html\" title=\"..\" class=\"link pic-item\">[/size][size=6]<imgdata original=\"//wx3.sinaimg.cn/large/0033dr8Dgy1grwj52dlbxj606o06ot8n02.jpg\" alt=\"..\"> <p>..</p> [/size][size=6]</a></div>[/size][size=6]<div class=\"col col-4 col-gutter-20\">[/size][size=6]<a href=\"/gif/3584251.html\" title=\"..\" class=\"link pic-item\"><img data-original=\"//wx3.sinaimg.cn/large/0033dr8Dgy1gqvp9xn0fqj61400u041f02.jpg\" alt=\"..\"> <p>..</p> </a></div>";
string start = "/gif/";
string last = ".html";
ls = new List<string>();
GetContent(a, start, last, 1);
}
public void GetContent(string str, string start, string last, int n)
{
if (str.ToLower().IndexOf(start.ToLower()) >= 0)
{
string str1 = str;
if (n == 1)
{
//保留关键字
int a1 = str.ToLower().IndexOf(start.ToLower());
str = str.Substring(str.ToLower().IndexOf(start.ToLower()), str.Length - str.ToLower().IndexOf(start.ToLower()));
int a2 = str.ToLower().IndexOf(last.ToLower());
if (a2 > -1)
{
str = str.Substring(0, str.ToLower().IndexOf(last.ToLower()) + last.Length);
ls.Add(str);
str1 = str1.Substring(a1 + a2 + last.Length);
}
}
else if (n == 2)
{
//去除关键字
int a1 = str.ToLower().IndexOf(start.ToLower()) + start.Length;
str = str.Substring(str.ToLower().IndexOf(start.ToLower()) + start.Length, str.Length - str.ToLower().IndexOf(start.ToLower()) - start.Length);
int a2 = str.ToLower().IndexOf(last.ToLower());
if (a2 > -1)
{
str = str.Substring(0, str.ToLower().IndexOf(last.ToLower()));
ls.Add(str);
str1 = str1.Substring(a1 + a2 + last.Length);
}
}
if (str1 != str)
{
GetContent(str1, start, last, n);
}
}
}
}
}
|