zoukankan      html  css  js  c++  java
  • [原创]一个查找并且替换的算法 Virus

    namespace ConsoleApp
    {
     
        
    class Program
        {

            
    public static void Main()
            {
                
    string input=Console.ReadLine();
                
    string output = Program.Replace (input, "ab""00");
                System.Console.ReadLine();

            }

            
    private static string Replace(string source, string a, string b)
            {
                
    string result = source;
                
    int position = 0;//发现的位置
                int len = 0;//
                int num = 0;//记录发现的要替换的个数

                
    for (int i = 0; i < result.Length - 1; i++)
                {
                    
    if (result[i] == a[0])
                    {
                        position 
    = i;
                        len 
    = 0;
                        
    //int j = 0;

                        
    while (len<=a.Length-1 && result[position + len] == a[len])
                        {
                            len
    ++;
                        }

                        
    if (len == a.Length)
                        {
                            Console.WriteLine(
    "find a positon");
                            num
    ++;
                            i 
    = position+len;
                            Console.WriteLine(
    "the posion is " + position + "\n the num is " + num);
                            
    continue;
                        }

                        
    if (a.Length == b.Length)
                        {

                            
    for (int j = position; j < a.Length - 1; j++)
                            {
                                result[j] 
    = a[j];
                            }
                        }
                        
    else if (a.Length > b.Length)
                        {
     
                        }
                        
    else
                        {

                        }
                    }
                }
                
    return "";
            }
        }
    }

    暂时实现了查找符合条件的个数和位置,替换的时候我想这样
    if (a.Length == b.Length)
                        {

                            for (int j = position; j < a.Length - 1; j++)
                            {
                                result[j] = a[j];
                            }
                        }
                        else if (a.Length > b.Length)
                        {
     
                        }
                        else
                        {

                        }
    要考虑三种情况,要查找的和要替换的字符串的长度相等,大于,小于。
    相等就可以直接替换相应位置的字符就可以了,可是发现错误
    ////..............................
     无法对属性或索引器“string.this[int]”赋值 -- 它是只读的  
    //.....................
    我的思路是搞一个数组存放没有替换的和替换了的所有字符,不知各位有没有更好的呢,在这里先谢谢你的回复!!

    【Blog】http://virusswb.cnblogs.com/

    【MSN】jorden008@hotmail.com

    【说明】转载请标明出处,谢谢

    反馈文章质量,你可以通过快速通道评论:

  • 相关阅读:
    个人网站
    物理读,逻辑读,预读
    正则表达式
    面向对象五大基本原则
    工作总结
    sql性能优化
    sqlServer游标的使用
    ASP.NET安全[开发ASP.NET MVC应用程序时值得注意的安全问题](转)
    一个简单问题引发对IEnumerable和IQueryable的思考
    EFCodeFirst 各种命令整理
  • 原文地址:https://www.cnblogs.com/virusswb/p/883964.html
Copyright © 2011-2022 走看看