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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Implement Find and replace (find given pattern and replace it with a given string)相关的知识,希望对你有一定的参考价值。

 

 

 

 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;
        }

reference:

http://blog.csdn.net/craiglin1992/article/details/44804983

以上是关于Implement Find and replace (find given pattern and replace it with a given string)的主要内容,如果未能解决你的问题,请参考以下文章

833. Find And Replace in String

890. Find and Replace Pattern

LC 890. Find and Replace Pattern

[LeetCode] 890. Find and Replace Pattern

833. Find And Replace in String

890. Find and Replace Pattern - LeetCode