匹配京东html
//通过正则匹配出,里面所有一级菜单的名字,拿到之后自己写一个对象。
//里面有一个字段叫firstMenue存的是一级菜单的数组,最后将json写到1.txt中
//创建一个大的列表
List<MyMenu> MyMenuList = new List<MyMenu>();
string files = File.ReadAllText("./京东.html");
//去掉所有非</li>结束标签后的换行符
files = Regex.Replace(files, "(?!</li)>\n", ">");
files = files.Replace("\n", "").Replace("</li>", "</li>\n");
files = files.Replace("\n", "这里是换行符");
string pattern2 = "<li\\s+class=\"cate_menu_item\".+>(.+)</li>";
MatchCollection matches = Regex.Matches(files, pattern2);
//a标签结尾 跟着换行符,
foreach (Match ii in matches)
{
if (ii.Success)
{
List<string> MenuList = new List<string>();
string Menu = Convert.ToString(ii);
Menu = Menu.Replace("</a>", "</a>\n");
string pattern3 = "<a.+class=\"cate_menu_lk\".+>(.+)</a>";
MatchCollection match = Regex.Matches(Menu, pattern3);
foreach (Match ii2 in match)//只遍历每一组中的a的内容
{
MenuList.Add(ii2.Groups[1].ToString());//拿到内容存到一个数组中
}
//这里是拿到了每一组菜单,把数组当做对象字段的值
MyMenuList.Add(MyMenu.GetMenu(MenuList));
}
}
//往文件中写入内容
string json = JsonMapper.ToJson(MyMenuList);
File.WriteAllText("./1.txt",json);
栈Stack
Stack stack = new Stack();
//入栈
stack.Push("jkkalsfkj");
stack.Push(34);
stack.Push(34.13);
//只能读取栈顶的数据
Console.WriteLine(stack.Peek());
//出栈
stack.Pop();
Console.WriteLine(stack.Peek());
//能不能查看有没有这个数据
Console.WriteLine(stack.Contains(34.13));
//遍历栈
while (stack.Count>0)
{
Console.WriteLine(stack.Peek());
stack.Pop();
}
//清空栈
stack.Clear();
stack.Push(34);
foreach (object i in stack)//使用foreach没有进行出栈的操作,所以栈还是原来的样子
{
Console.WriteLine(i);
}
Console.WriteLine(stack.Peek());
编写一个方法计算一个十进制数的二进制数,使用栈的结构方式存储,之后打印出来
int dec = 1023;
Stack decStack = new Stack();
while (dec!=0&&dec!=1)
{
int a = dec%2;
decStack.Push(a);
dec /= 2;
}
foreach (int i in decStack)
{
Console.WriteLine(i);
}
队列Queue
Queue是一种先进先出的数据结构,就像我们排队一样,先排在前面的人先出去
//创建一个队列
Queue<string> queue = new Queue<string>(); //可以限定数据类型
//入队列
queue.Enqueue("hello");
queue.Enqueue("hello2");
// queue.Enqueue(12);//无法入队
//查看队头的值
Console.WriteLine(queue.Peek());
// 查看某个数据是否在队中
Console.WriteLine(queue.Contains("hello"));
//出队列
queue.Dequeue();
Console.WriteLine(queue.Peek());
queue.Enqueue("nihao");
queue.Enqueue("nihao1");
queue.Enqueue("nihao2");
queue.Enqueue("nihao3");
//队列是无法修改其中的值的
Console.WriteLine("\n");
//遍历的操作
foreach (string i in queue)
{
Console.WriteLine(i);
}
Console.WriteLine(queue.Peek());
Console.WriteLine();
while (queue.Count>0)
{
Console.WriteLine(queue.Peek());
queue.Dequeue();
}
queue.Clear();
使用队列存储消息,一次性存10条消息,每隔一秒打印一条消息,每一秒使用Thread.sleep(1000)
//使用队列存储消息,一次性存10条消息,每隔一秒打印一条消息,
//每一秒使用Thread.sleep(1000)
Random rnd = new Random();
List<string> news = new List<string>()
{
"我的朋友","好的","你好"
};
Queue queue1 = new Queue();
for (int i = 0; i < 10; i++)
{
string str = news[rnd.Next(news.Count)];
queue.Enqueue(str);
}
while (queue.Count>0)
{
Console.WriteLine(queue.Peek());
queue.Dequeue();
Thread.Sleep(1000);
}
哈希表
Hashtable hashtable = new Hashtable();
hashtable.Add("name", "xiaoming");
hashtable.Add("age", 13);
hashtable.Add("high", 190);
try
{
hashtable.Add("name", "xiaoming"); //键名在哈希表中是唯一的
}
catch (Exception)
{
Console.WriteLine("键名重复");
//throw;
}
Console.WriteLine(hashtable["name"]);//通过键名找到后面的值,如果没有该键名返回空
hashtable.Remove("high");//删除
//查看是否含有键或值
Console.WriteLine(hashtable.ContainsKey("high"));
Console.WriteLine(hashtable.Contains("high"));
Console.WriteLine(hashtable.ContainsValue(13));
//如何遍历哈希表
foreach (object i in hashtable.Keys)
{
Console.WriteLine(i);
}
foreach (object i in hashtable.Values)
{
Console.WriteLine(i);
}
foreach (DictionaryEntry i in hashtable)
{
Console.WriteLine(i.Key);
Console.WriteLine(i.Value);
}
Console.WriteLine("-------------------------");