【问题标题】:Searching a String for a certain thing, then removing up to a certain point to a list在字符串中搜索某件事,然后删除到列表中的某个点
【发布时间】:2011-04-01 16:15:29
【问题描述】:

我正在开发一个供个人使用的自动下载器,到目前为止,我已经设法将程序设置为将提供的链接源存储到字符串中,下载链接以纯文本形式写入来源,所以我需要做的是搜索一个字符串,比如“http://media.website.com/folder/”,然后让它返回所有出现的列表?问题是,我还需要在 /folder/ 之后为每个文件提供的唯一 ID 与上述每次出现一起存储,有什么想法吗?我使用 Visual C#。

谢谢!!!

史蒂文

【问题讨论】:

  • 使用 Regex.Match( input, "your expression here" )

标签: c# string search


【解决方案1】:

也许是这样的?

        Dictionary<string, string> dictionary = new Dictionary<string, string>();

        string searchText = "Text to search here";
        string textToFind = "Text to find here";
        string fileID = "";

        bool finished = false;
        int foundIndex = 0;

        while (!finished)
        {
            foundIndex = searchText.IndexOf(textToFind, foundIndex);

            if (foundIndex == -1)
            {
                finished = true;
            }
            else
            {
                //get fieID, change to whatever logic makes sense, in this example
                //it assumes a 2 character identifier following the search text
                fileID = searchText.Substring(foundIndex + searchText.Length, 2);

                dictionary.Add(fileID, textToFind);
            }
        }

【讨论】:

    【解决方案2】:

    使用 Regex 获取匹配项,这将为您提供所有匹配项的列表。对匹配之间不同的数值使用通配符,以便您对其进行解析。

    我不擅长正则表达式,但它会是这样的,

    Regex.Match(<your string>,@"(http://media.website.com/folder/)(d+)")
    

    【讨论】:

      【解决方案3】:

      或者

      var textToFind = "http://media.website.com/folder/";
      var ids = from l in listOfUrls where l.StartsWith(textToFind) select new { RawUrl = l, ID=l.Substring(textToFind.Length)}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-05-04
        • 1970-01-01
        • 2013-08-04
        • 2012-11-11
        • 1970-01-01
        • 2020-06-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多