我需要比较两个字符列表数组 [英] I need to compare two char list arrays

查看:77
本文介绍了我需要比较两个字符列表数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试检查网页上的特定链接,以确定该链接是否在该页面上.我正在尝试将该tagList转换为char数组和字符串url 1st,然后我检查它是否相等(在某种意义上说char等于char),而不是逐字符串.我希望它检查我已经用任何页面上所有可用内容进行硬编码的URL.一旦得到我硬编码的文件,应该说找到了.如果可以在此代码中进行任何更改,或者如果我缺少某处,请提供帮助.
问候.

Hi, i am trying to check on a webpage for a specific link that whether it is on that page or not. I am trying to convert that tagList to char array and the string url 1st and then i am checking whether it is equal or not(equal in a sense char by char), not by string by string. I want it to check the url i already have hard coded with the all available on any page. As soon as it gets the one i hard coded it should say found. Kindly help if there can be made any changes in this code, or if somewhere i am missing.
Regards.

private void tags_get()
        {

            List<string> tagList = new List<string>();

            List<string> linkList = new List<string>();

            //char[] c_arr;

            string url="http://www.adireo.com/";
            List<char> newUrl = new List<char>(url);
            //Convert.ToChar(newUrl);
            foreach (HtmlElement link in webBrowserCtl.Document.Links)
            {

                linkList.Add(link.GetAttribute("href").ToString());

                tagList.Add(link.InnerText);

            }

            List<string> new_tagList = new List<string>(tagList);
            //Convert.ToChar(new_tagList);
            List<char> url_converted = new List<char>(newUrl);
            for (int a = 0; a < url_converted.Count; a++)

            {

                if (!(new_tagList).Contains(url_converted))

                {

                    MessageBox.Show("found");

                }

                else

                {

                    MessageBox.Show("Not Found");

                }

            }

           }

          }

推荐答案

您可以使用SequenceEqual,因为您正在使用c#4.0

检查一下.

http://msdn.microsoft.com/en-us/library/bb348567.aspx [ ^ ]

或者如果您想要自己的方法,请尝试此操作.
You can use SequenceEqual since you are using c#4.0

Check this.

http://msdn.microsoft.com/en-us/library/bb348567.aspx[^]

or if you want your own method, try this.
static bool ArraysEqual<T>(T[] a1, T[] a2)
{
    if (ReferenceEquals(a1,a2))
        return true;

    if (a1 == null || a2 == null)
        return false;

    if (a1.Length != a2.Length)
        return false;

    EqualityComparer<T> comparer = EqualityComparer<T>.Default;
    for (int i = 0; i < a1.Length; i++)
    {
        if (!comparer.Equals(a1[i], a2[i])) return false;
    }
    return true;
}


http://stackoverflow.com/questions/4347468/net-char-array-comparison [ ^ ]
http://stackoverflow.com/questions/996233/c-sharp-how-to-equate-the-elements-of-two-array[^]
http://stackoverflow.com/questions/4347468/net-char-array-comparison[^]


为澄清起见,它出现是在检查链接的文本是否匹配url,而不是链接的目标链接匹配?
像这样的链接:
< a href ="http://google.com/"> http://www.adireo.com/</a>
应该是匹配?

我看不出您需要逐个字符比较的原因. StringComparison.Ordinal将进行最精确的比较.

如果您只想对某些链接匹配url的是非进行尝试,请尝试如下操作:
To clarify, it appears you are checking if the text of the link matches url, not if the target of the link matches?
So a link like:
<a href="http://google.com/">http://www.adireo.com/</a>
should be a match?

I don''t see the reason that you need to compare char-by-char. StringComparison.Ordinal will do the most-exact comparison.

If you just want a true/false that some link matches url try something like this:
// requires: using System.Linq;
private void tags_get()
{
  const string url="http://www.adireo.com/";
  var links = webBrowserCtl.Document.Links;
  bool found = links.Any(link => string.Equals(link.InnerText, url, System.StringComparison.Ordinal));
  // or, if you really intend to compare the link target:
  //bool found = links.Any(link => string.Equals(link.GetAttribute("href"), url, System.StringComparison.Ordinal));
  MessageBox.Show(found ? "found" : "Not Found");
}


假设您要在确定url不存在之后对链接做一些事情,您可以获取由 text target 键控的链接字典>:


Assuming you want to do something with the links after determining that url is/isn''t present, you could get a Dictionary of the links keyed by the text or target:

private void tags_get()
{
  Dictionary<string, HtmlElement> linkMap = new Dictionary<string, HtmlElement>(StringComparer.Ordinal);
  const string url="http://www.adireo.com/";
  foreach (HtmlElement link in webBrowserCtl.Document.Links)
  {
    string key = link.InnerText;  // link text
    // or
    //string key = link.GetAttribute("href"); // link target
    if (linkMap.ContainsKey(key))
    {
      // this link occurs multiple times, handle appropriately!
    }
    linkMap.Add(key, link);
  }

  if (linkMap.ContainsKey(url))
  {
    MessageBox.Show("found");
  }
  else
  {
    MessageBox.Show("Not Found");
  }
  // do additional processing of the links ...
}


这篇关于我需要比较两个字符列表数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆