首页 > 其他 > 详细

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

时间:2016-01-25 06:32:24      阅读:175      评论:0      收藏:0      [点我收藏+]

 

 

 

 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)

原文:http://www.cnblogs.com/hygeia/p/5156485.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!