字符串之Raplace函数的实现
//这是普通的repalce的方法,不知道c#中的string如何实现的
public static string Repalce(string str, string oldValue, string newValue)
{
for (int i = 0; i < str.Length - oldValue.Length; i++)
{
if (str.Substring(i, oldValue.Length) == oldValue)
{
string forStr = str.Substring(0, i);
string afterStr = str.Substring(i + oldValue.Length);
str = forStr + newValue + afterStr;
i = i + newValue.Length - 1;
}
}
return str;
}
//递归实现
public static string RepalceD(string Str, string oldValue, string newValue)
{
int index = Str.IndexOf(oldValue);
if (index == -1)
{
return Str;
}
else
{
return Str.Substring(0, index) + newValue + RepalceD(Str.Substring(index + oldValue.Length), oldValue, newValue);
}
}
public static string Repalce(string str, string oldValue, string newValue)
{
for (int i = 0; i < str.Length - oldValue.Length; i++)
{
if (str.Substring(i, oldValue.Length) == oldValue)
{
string forStr = str.Substring(0, i);
string afterStr = str.Substring(i + oldValue.Length);
str = forStr + newValue + afterStr;
i = i + newValue.Length - 1;
}
}
return str;
}
//递归实现
public static string RepalceD(string Str, string oldValue, string newValue)
{
int index = Str.IndexOf(oldValue);
if (index == -1)
{
return Str;
}
else
{
return Str.Substring(0, index) + newValue + RepalceD(Str.Substring(index + oldValue.Length), oldValue, newValue);
}
}
浙公网安备 33010602011771号