Johnny with dotnet

字符串之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);
            }
        }

 

posted on 2010-07-13 17:39  JohnnyNet  阅读(372)  评论(0)    收藏  举报

导航