[LinkedIn]Implement Find and replace (find given pattern and replace it with a given string)

From Here

 public static String FindAndReplace(String GivenString, String Pattern, String ReplaceString)
        {
            int j = 0;
            int tempi;

            for (int i = 0; i < GivenString.Length; i++)
            {
                tempi = i;
                j = 0;
                while (i<GivenString.Length && j< Pattern.Length && GivenString[i] == Pattern[j])//trying to match the pattern
                {
                    i++;
                    j++;
                }
                if (j == Pattern.Length)//if we find the given pattern
                {
                    GivenString = GivenString.Substring(0, tempi) + ReplaceString + GivenString.Substring(i, GivenString.Length - i);
                    i = tempi + ReplaceString.Length-1;
                    continue; 
                }
                //if we didn't find it, set i back
                i = tempi;
            }
            return GivenString;
        }
posted on 2015-04-01 12:07  Seth_L  阅读(110)  评论(0编辑  收藏  举报