帮助递归和返回值
这是之前发布的问题的扩展。我正在尝试递归地构建一个字符串。我需要更改下面的函数来执行此操作 - 函数的每次递归都会生成所需的字符串,但我需要将它们连接在一起并返回整个字符串。 “相关”作为空字符串传递到函数中,我认为我使用 string.Format 的方式会将每个递归附加到“相关”字符串吗?显然不是。
不知道如何...
private string getRelatedNews(Taxonomy taxData, string related, string contentTitle)
{
foreach (TaxonomyItemData item in taxData.TaxonomyItems)
{
if (taxData.TaxonomyName.Equals(contentTitle) && taxData.TaxonomyItemCount != 0)
{
related = string.Format("{0}<li><a href='{1}'\">{2}</a></li>", related, item.Link, item.Name);
}
}
// Show all its sub categories
foreach (TaxonomyData cat in taxData.Taxonomy)
{
getRelatedNews(cat, related, contentTitle);
}
return(related);
}
This is an extension of a previously posted question. I'm trying to recursively build a string. I need to change the function below to do this - each recursion of the function generates the desired string, but I need to concat these together and return the whole string. 'related' is passed into the function as an empty string and I thought the way I was using string.Format would append each recursion to the 'related' string? Apparently not.
Not sure how...
private string getRelatedNews(Taxonomy taxData, string related, string contentTitle)
{
foreach (TaxonomyItemData item in taxData.TaxonomyItems)
{
if (taxData.TaxonomyName.Equals(contentTitle) && taxData.TaxonomyItemCount != 0)
{
related = string.Format("{0}<li><a href='{1}'\">{2}</a></li>", related, item.Link, item.Name);
}
}
// Show all its sub categories
foreach (TaxonomyData cat in taxData.Taxonomy)
{
getRelatedNews(cat, related, contentTitle);
}
return(related);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
应该是
因为字符串是不可变的。
should be
because strings are immutable.
好吧,试试这个......
我不确定你的逻辑和程序流程......然后我也认为递归函数必须这样调用......
Well try this...
I am not sure about your logic and flow of the program... then also I think recursive function must be called like this....