C# 如何迭代多个变量?

C# 如何迭代多个变量?,c#,iteration,C#,Iteration,我想创建一个简短的程序,从网站下载几张图片 在表单中,我想输入一个带有占位符的网站的根链接。 占位符可以用开始/结束值和asc/描述来定义 例如:原始链接是 google.de/1236-01.jpg 我想从中生成所有链接 google.de/1236-1.jpg 最多 google.de/9955-12.jpg 因此,我的输入将是“google.de/[0]-[1].jpg”,占位符设置为: [0] = start 1236|end 9955|asc [1] = start 1|end 12|

我想创建一个简短的程序,从网站下载几张图片

在表单中,我想输入一个带有占位符的网站的根链接。
占位符可以用开始/结束值和asc/描述来定义

例如:原始链接是

google.de/1236-01.jpg

我想从中生成所有链接

google.de/1236-1.jpg

最多

google.de/9955-12.jpg

因此,我的输入将是“google.de/[0]-[1].jpg”,占位符设置为:

[0] = start 1236|end 9955|asc
[1] = start 1|end 12|asc

通过
GetValidCharacters()
我获得每个输入占位符的有效组合的字符串列表(可以通过升序/降序+开始和结束进行选择)

我的目标是构建这个链接的所有组合,因为我需要在运行时确定我有多少占位符

我的想法是循环一个队列,然后
enquoue
每一个新的构建行,直到没有剩余的占位符,但我不知道如何做到这一点

我需要确保所有组合都已输入,并且只输入一次

private static void CreateDownloadList()
{
    Queue<string> tmpQueue = new Queue<string>(); //temp queue
    tmpQueue.Enqueue(DL_path); //DL_Path = google.de/[0]-[1].jpg
    string line = "";
    while ((line = tmpQueue.Dequeue()) != null) //not empty
    {
        if (line.Contains("[")) //placeholder
        {
            string tmpLine = line;
            //how to determine, which placeholder is next?? need to know this and replace this with every combination, I get from GetValidCharacters(start, end, DESC)
        }
        else //done
        {
            _urlList.Add(line);
        }
    }
}
private static void CreateDownloadList()
{
队列TMPQUE=新队列();//临时队列
tmpQueue.Enqueue(DL_path);//DL_path=google.de/[0]-[1].jpg
字符串行=”;
while((line=tmpQueue.Dequeue())!=null)//不为空
{
if(line.Contains(“[”)//占位符
{
字符串tmpLine=行;
//如何确定下一个占位符??需要知道这一点并用每个组合替换它,我从GetValidCharacters(开始、结束、描述)获得
}
否则//完成
{
_添加(行);
}
}
}

一个简单的
循环怎么样

for (int i = 1236; i <= 9955; i++)
{
    for (int j = 1; j <= 12; j++)
    {
        tmpQueue.Enqueue(string.Format("google.de/{0}-{1}.jpg", i, j)); 
    }
}

for(inti=1236;i我不会给出完整的代码,但是这里有一些伪代码可以解决这个问题

鉴于:

todostack -- stack object that holds a list of unresolved items

replace_map -- map object that holds marker string and map of all values

marker_list -- list of all markers

final_list -- list object that holds the results
(注意,您可能可以在一个对象中使用marker_list和replace_map——我将它们分开以使代码更清晰)

初始化:

push todostack with your starting string

set marker_list and replace_map to correct values (from parameters I assume)

clear final_list
算法:

 while (there are any items in todostack)
 {       
    curitem = todostack.pop
    if (curitem contains a marker in marker_list)
    {
       loop for each replacement in replace_map
       {
          new_item = curitem replaced with replacement
          todostack.push(new_item)
       }
    }
    else
      add curitem to final_list
 }

@霍根:这是正确方法的暗示。 解决办法是这样的

private void CreateDownloadList()
        {
            Queue<string> tmpQueue = new Queue<string>();
            tmpQueue.Enqueue(downloadPathWithPlaceHolders);
            while(tmpQueue.Count > 0)
            {
                string currentItem = tmpQueue.Dequeue();
                bool test = false;
                if(currentItem.Contains("["))
                {
                    foreach(Placeholder p in _placeholders)
                    {
                        if(currentItem.Contains(p.PlaceHolder))
                        {
                            foreach(string s in p.Replacements)
                            {
                                tmpQueue.Enqueue(currentItem.Replace(p.PlaceHolder, s));
                            }
                            test = true;
                        }
                        if(test)
                            break;
                    }
                }
                else
                {
                    _downloadLinkList.Add(currentItem);
                }
            }
        }
private void CreateDownloadList()
{
队列TMPQUE=新队列();
tmpQueue.Enqueue(带占位符的下载路径);
而(tmpQueue.Count>0)
{
字符串currentItem=tmpQueue.Dequeue();
布尔检验=假;
if(currentItem.Contains(“[”))
{
foreach(占位符p在_占位符中)
{
if(currentItem.Contains(p.PlaceHolder))
{
foreach(p.Replacements中的字符串s)
{
tmpQueue.Enqueue(currentItem.Replace(p.PlaceHolder,s));
}
测试=真;
}
如果(测试)
打破
}
}
其他的
{
_downloadLinkList.Add(当前项);
}
}
}

因为您使用的是占位符,所以只需使用
字符串。替换(“[0]”,随便什么)
。您可以选择(实际上不需要,但可以用于停止循环)使用
字符串。Contains(“[10]”
)检查是否存在此类占位符。我认为这并不能解决所描述的问题——问题需要“动态的”传入的占位符。在运行时之前,您不知道有多少占位符或值是什么。但他可以通过参数将范围传递到方法中,或者您的代码示例中的动态类型是什么您有一个常量字符串
google.de/{0}-{1}.jpg
和常量范围1-12以及1236到9955。如果我希望它执行
测试{0}、{1}、{2}
以及范围1-5、3-9和1234到21345,该怎么办?