C#-如何停止递归函数?
我想用另一个单词替换所有以C#-如何停止递归函数?,c#,C#,我想用另一个单词替换所有以@开头的单词,以下是我的代码: public string SemiFinalText { get; set; } public string FinalText { get; set; } //sample text : "aaaa bbbb @cccc dddd @eee fff g" public string GetProperText(string text) { if (text.Contains(
@
开头的单词,以下是我的代码:
public string SemiFinalText { get; set; }
public string FinalText { get; set; }
//sample text : "aaaa bbbb @cccc dddd @eee fff g"
public string GetProperText(string text)
{
if (text.Contains('@'))
{
int index = text.IndexOf('@');
string restText = text.Substring(index);
var indexLast = restText.IndexOf(' ');
var oldName = text.Substring(index, indexLast);
string restText2 = text.Substring( index + indexLast);
SemiFinalText += text.Substring(0, index + indexLast).Replace(oldName, "@New");
if (restText2.Contains('@'))
{
GetProperText(restText2);
}
FinalText = SemiFinalText + restText2;
return FinalText;
}
else
{
return text;
}
}
当返回FinalText时代码>被执行,我想停止递归函数。你怎么能修好它
也许另一种方法比递归函数更好。如果你知道另一种方法,请给我一个答案
这个问题不需要递归解决方案。您有一个包含多个单词(用空格分隔)的字符串,您希望用另一个字符串替换以“@”开头的单词。修改您的解决方案,使其具有基于空格拆分的简单方法,替换以@开头的所有单词,然后再次组合它们
使用Linq:
string text = "aaaa bbbb @cccc dddd @eee fff g";
FinalText = GetProperText(text, "New");
public string GetProperText(string text, string replacewith)
{
text = string.Join(" ", text.Split(' ').Select(x => x.StartsWith("@") ? replacewith: x));
return text;
}
Regex rgx = new Regex("@([^ @])*");
string result = rgx.Replace(text, replaceword);
输出:aaaa bbbb新dddd新fff g
使用正则表达式:
string text = "aaaa bbbb @cccc dddd @eee fff g";
FinalText = GetProperText(text, "New");
public string GetProperText(string text, string replacewith)
{
text = string.Join(" ", text.Split(' ').Select(x => x.StartsWith("@") ? replacewith: x));
return text;
}
Regex rgx = new Regex("@([^ @])*");
string result = rgx.Replace(text, replaceword);
这个问题不需要递归解决方案。您有一个包含多个单词(用空格分隔)的字符串,您希望用另一个字符串替换以“@”开头的单词。修改您的解决方案,使其具有基于空格拆分的简单方法,替换以@开头的所有单词,然后再次组合它们
使用Linq:
string text = "aaaa bbbb @cccc dddd @eee fff g";
FinalText = GetProperText(text, "New");
public string GetProperText(string text, string replacewith)
{
text = string.Join(" ", text.Split(' ').Select(x => x.StartsWith("@") ? replacewith: x));
return text;
}
Regex rgx = new Regex("@([^ @])*");
string result = rgx.Replace(text, replaceword);
输出:aaaa bbbb新dddd新fff g
使用正则表达式:
string text = "aaaa bbbb @cccc dddd @eee fff g";
FinalText = GetProperText(text, "New");
public string GetProperText(string text, string replacewith)
{
text = string.Join(" ", text.Split(' ').Select(x => x.StartsWith("@") ? replacewith: x));
return text;
}
Regex rgx = new Regex("@([^ @])*");
string result = rgx.Replace(text, replaceword);
正则表达式的解决方案:
using System;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
string pattern = @"@\w+";
var r = new Regex(pattern);
Console.WriteLine(r.Replace("ABC @ABC ABC @DEF klm.@bhsh", "BOOM!"));
}
}
这不依赖空格字符作为分隔符,任何非单词(字母和数字)都可以用来分隔“单词”。此示例输出:
美国广播公司加油!美国广播公司加油!轰
您可以在这里进行测试:
如果您是正则表达式新手:正则表达式解决方案:
using System;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
string pattern = @"@\w+";
var r = new Regex(pattern);
Console.WriteLine(r.Replace("ABC @ABC ABC @DEF klm.@bhsh", "BOOM!"));
}
}
这不依赖空格字符作为分隔符,任何非单词(字母和数字)都可以用来分隔“单词”。此示例输出:
美国广播公司加油!美国广播公司加油!轰
您可以在这里进行测试:
如果您对Regex不熟悉:这里还有一个合适的方法,可以递归地执行它。我认为您的停止条件实际上是OK,但是您应该将递归函数调用的结果连接到已处理的文本。我还认为在递归函数中使用全局变量有点违背了它的目的
尽管如此,我认为使用所提供答案之一的正则表达式更好更快
递归代码:
//sample text : "aaaa bbbb @cccc dddd @eee fff g"
public string GetProperText(string text)
{
if (text.Contains('@'))
{
int index = text.IndexOf('@'); //Index of first occuring '@'
var indexLast = text.IndexOf(' ',index); //Index of first ' ' after '@'
var oldName = text.Substring(index, indexLast); //Old Name
string processedText = text.Substring(0, index + indexLast).Replace(oldName, "New"); //String with new name
string restText = text.Substring(indexLast); //Rest Text
if (text.Contains('@'))
{
//Here the outcome of the function is pasted on the allready processed text part.
text = processedText + GetProperText(restText);
}
return text;
}
else
{
return text;
}
}
这里还有一种适合感兴趣的人递归执行的方法。我认为您的停止条件实际上是OK,但是您应该将递归函数调用的结果连接到已处理的文本。我还认为在递归函数中使用全局变量有点违背了它的目的
尽管如此,我认为使用所提供答案之一的正则表达式更好更快
递归代码:
//sample text : "aaaa bbbb @cccc dddd @eee fff g"
public string GetProperText(string text)
{
if (text.Contains('@'))
{
int index = text.IndexOf('@'); //Index of first occuring '@'
var indexLast = text.IndexOf(' ',index); //Index of first ' ' after '@'
var oldName = text.Substring(index, indexLast); //Old Name
string processedText = text.Substring(0, index + indexLast).Replace(oldName, "New"); //String with new name
string restText = text.Substring(indexLast); //Rest Text
if (text.Contains('@'))
{
//Here the outcome of the function is pasted on the allready processed text part.
text = processedText + GetProperText(restText);
}
return text;
}
else
{
return text;
}
}
对于“aaaa bbbb[at]cccc dddd[at]eee fff g”的示例输入,您期望的输出是什么?我想用另一个特定的单词替换所有以[at]开头的单词。@user3748973您希望exlusive是递归的听起来是一个很好的选择。如果您不熟悉正则表达式强大的模式匹配功能,我可以提供更详细的信息。当然,请解释一下。对于“aaaa bbbb[at]cccc dddd[at]eee fff g”的示例输入,您期望的输出是什么?我想替换所有以[at]开头的单词使用另一个特定的词。@user3748973您希望exlusive是recursive听起来是一个很好的候选词。如果你不熟悉正则表达式强大的模式匹配功能,我可以提供更多的细节。当然,请解释一下。如果我们有这样的字符串:aaa bbb.@ccc ddd
,它不起作用。你有什么解决办法吗?你想把这个@ccc也换掉吗?如果我们有这样的字符串:aaa bbb.@ccc ddd
它不起作用。你有什么解决办法吗?你想把这个@ccc也换掉吗?