批量字符串替换程序

        很多时候我们需要用到字符串替换程序,比如插入数据库时需要将'替换成''。当只有少数待换字符串时用String.Replace性能很高,但因为其复杂度是O(n),当待换字符串很多的时候,String.Replace的性能就降下来了。前些天写了几个程序,用于批量替换字符串。

问题:批量替换字符串。定义字符串对PairString{OldValue,NewValue}。
                                OldValue<------>NewValue 
            称---->为右过滤,简称过滤。<----为左过滤,或反过滤。
            给定字符串S,一个PairString数组,用PairString数组过滤S(左、右可设置)。

程序实现:

(1)PairString

 1 None.gif      public   struct  PairString
 2 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
 3InBlock.gif        public string OldValue;
 4InBlock.gif        public string NewValue;
 5InBlock.gif        public bool IsRightFilter;
 6InBlock.gif        public bool IsLeftFilter;
 7InBlock.gif
 8InBlock.gif        public PairString(string oldValue, string newValue)
 9ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
10InBlock.gif            OldValue=oldValue;
11InBlock.gif            NewValue=newValue;
12InBlock.gif            IsRightFilter=true;
13InBlock.gif            IsLeftFilter=true;
14ExpandedSubBlockEnd.gif        }

15InBlock.gif
16InBlock.gif        public PairString(string oldValue, string newValue, bool isRightFilter, bool isLeftFilter)
17ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
18InBlock.gif            OldValue=oldValue;
19InBlock.gif            NewValue=newValue;
20InBlock.gif            IsRightFilter=isRightFilter;
21InBlock.gif            IsLeftFilter=isLeftFilter;
22ExpandedSubBlockEnd.gif        }

23ExpandedBlockEnd.gif    }


(2)采用Replace实现

 1 None.gif      public   class  StringFilter
 2 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
 3InBlock.gif        private PairString[] _filterSet=null;
 4InBlock.gif
 5InBlock.gif        public PairString[] FilterSet
 6ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 7InBlock.gif            get
 8ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 9InBlock.gif                return _filterSet;
10ExpandedSubBlockEnd.gif            }

11InBlock.gif            set
12ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
13InBlock.gif                _filterSet=value;
14ExpandedSubBlockEnd.gif            }

15ExpandedSubBlockEnd.gif        }

16InBlock.gif
17InBlock.gif
18InBlock.gif        public StringFilter()
19ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
20ExpandedSubBlockEnd.gif        }

21InBlock.gif
22InBlock.gif
23InBlock.gif        public virtual  string Filter(string input)
24ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
25InBlock.gif            foreach (PairString p in FilterSet)
26ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
27InBlock.gif                if(p.IsRightFilter==true) input=input.Replace(p.OldValue,p.NewValue);
28ExpandedSubBlockEnd.gif            }

29InBlock.gif            return input;
30ExpandedSubBlockEnd.gif        }

31InBlock.gif
32InBlock.gif        public virtual string AntiFilter(string input)
33ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
34InBlock.gif            foreach (PairString p in FilterSet)
35ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
36InBlock.gif                if(p.IsLeftFilter==true) input=input.Replace(p.NewValue,p.OldValue);
37ExpandedSubBlockEnd.gif            }

38InBlock.gif            return input;
39ExpandedSubBlockEnd.gif        }

40ExpandedBlockEnd.gif    }

(3)批量替换。我最先采用Hashtable实现,测试结果发现性能比较低,然后就直接改用数组实现,写一个hash函数,将待换字符串hash到数组。flexIndex是松弛系数。

  1 None.gif      public   class  StringBatchFilter:StringFilter
  2 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
  3InBlock.gif        private int width=0;
  4InBlock.gif        const int flexIndex = 10;
  5InBlock.gif
  6InBlock.gif        private object[] _rightArray;
  7InBlock.gif        private object[] _leftArray;
  8InBlock.gif
  9InBlock.gif        public object[] RightArray
 10ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 11InBlock.gif            get
 12ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 13InBlock.gif                return _rightArray;
 14ExpandedSubBlockEnd.gif            }

 15InBlock.gif            set
 16ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 17InBlock.gif                _rightArray = value;
 18ExpandedSubBlockEnd.gif            }

 19ExpandedSubBlockEnd.gif        }

 20InBlock.gif
 21InBlock.gif        public object[] LeftArray
 22ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 23InBlock.gif            get
 24ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 25InBlock.gif                return _leftArray;
 26ExpandedSubBlockEnd.gif            }

 27InBlock.gif            set
 28ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 29InBlock.gif                _leftArray = value;
 30ExpandedSubBlockEnd.gif            }

 31ExpandedSubBlockEnd.gif        }

 32InBlock.gif
 33InBlock.gif        public StringBatchFilter()
 34ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 35ExpandedSubBlockEnd.gif        }

 36InBlock.gif
 37InBlock.gif        public void Init()
 38ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 39InBlock.gif            width=flexIndex*FilterSet.Length;
 40InBlock.gif            RightArray = new object[width];
 41InBlock.gif            LeftArray = new object[width];
 42InBlock.gif
 43InBlock.gif            foreach (PairString p in FilterSet)
 44ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 45InBlock.gif                if(p.IsRightFilter==true)
 46ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 47InBlock.gif                    if(RightArray[p.OldValue[0]%width]==null)
 48ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 49InBlock.gif                        RightArray[p.OldValue[0]%width]=new ArrayList();
 50ExpandedSubBlockEnd.gif                    }

 51InBlock.gif                    ((ArrayList)RightArray[p.OldValue[0]%width]).Add(p);
 52InBlock.gif
 53ExpandedSubBlockEnd.gif                }

 54InBlock.gif
 55InBlock.gif                if(p.IsLeftFilter==true)
 56ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 57InBlock.gif                    if(LeftArray[p.NewValue[0]%width]==null)
 58ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 59InBlock.gif                        LeftArray[p.NewValue[0]%width]=new ArrayList();
 60ExpandedSubBlockEnd.gif                    }

 61InBlock.gif                    else
 62ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 63InBlock.gif                        ((ArrayList)LeftArray[p.NewValue[0]%width]).Add(p);
 64ExpandedSubBlockEnd.gif                    }

 65ExpandedSubBlockEnd.gif                }

 66ExpandedSubBlockEnd.gif            }

 67ExpandedSubBlockEnd.gif        }

 68InBlock.gif
 69InBlock.gif        public override string Filter(string input)
 70ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 71InBlock.gif            StringBuilder sb = new StringBuilder((int)(input.Length*1.2));
 72InBlock.gif
 73InBlock.gif            for (int i=0; i< input.Length; )
 74ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 75InBlock.gif                int m = input[i]%width;
 76InBlock.gif                ArrayList l = (ArrayList)RightArray[m];
 77InBlock.gif                if(l!=null)
 78ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 79InBlock.gif                    for(int j=0;j<l.Count;j++)
 80ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 81InBlock.gif                        PairString p = (PairString)l[j];
 82InBlock.gif                        if(((i+p.OldValue.Length)<input.Length))
 83ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
 84InBlock.gif                            if(input.Substring(i,p.OldValue.Length)==p.OldValue)
 85ExpandedSubBlockStart.gifContractedSubBlock.gif                            dot.gif{
 86InBlock.gif                                sb.Append(p.NewValue);
 87InBlock.gif                                i+=p.OldValue.Length;
 88InBlock.gif                                goto label;
 89ExpandedSubBlockEnd.gif                            }

 90ExpandedSubBlockEnd.gif                        }

 91ExpandedSubBlockEnd.gif                    }

 92InBlock.gif                    sb.Append(input[i]);
 93InBlock.gif                    i++;
 94InBlock.gif                label:
 95InBlock.gif                    ;
 96ExpandedSubBlockEnd.gif                }

 97InBlock.gif                else
 98ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 99InBlock.gif                    sb.Append(input[i]);
100InBlock.gif                    i++;
101ExpandedSubBlockEnd.gif                }

102ExpandedSubBlockEnd.gif            }

103InBlock.gif            return sb.ToString();
104ExpandedSubBlockEnd.gif        }

105InBlock.gif
106InBlock.gif        public override string AntiFilter(string input)
107ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
108InBlock.gif            StringBuilder sb = new StringBuilder((int)(input.Length*1.2));
109InBlock.gif
110InBlock.gif            for (int i=0; i< input.Length; )
111ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
112InBlock.gif                int m = input[i]%width;
113InBlock.gif                ArrayList l = (ArrayList)LeftArray[m];
114InBlock.gif                if(l!=null)
115ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
116InBlock.gif                    for(int j=0;j<l.Count;j++)
117ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
118InBlock.gif                        PairString p = (PairString)l[j];
119InBlock.gif                        if(((i+p.NewValue.Length)<input.Length))
120ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
121InBlock.gif                            if(input.Substring(i,p.NewValue.Length)==p.NewValue)
122ExpandedSubBlockStart.gifContractedSubBlock.gif                            dot.gif{
123InBlock.gif                                sb.Append(p.OldValue);
124InBlock.gif                                i+=p.NewValue.Length;
125InBlock.gif                                goto label;
126ExpandedSubBlockEnd.gif                            }

127ExpandedSubBlockEnd.gif                        }

128ExpandedSubBlockEnd.gif                    }

129InBlock.gif                    sb.Append(input[i]);
130InBlock.gif                    i++;
131InBlock.gif                label:
132InBlock.gif                    ;
133ExpandedSubBlockEnd.gif                }

134InBlock.gif                else
135ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
136InBlock.gif                    sb.Append(input[i]);
137InBlock.gif                    i++;
138ExpandedSubBlockEnd.gif                }

139ExpandedSubBlockEnd.gif            }

140InBlock.gif            return sb.ToString();
141ExpandedSubBlockEnd.gif        }

142ExpandedBlockEnd.gif    }

(4)例子

 1 None.gif     StringBatchFilter sk  =   new  StringBatchFilter();
 2 None.gif
 3 None.gif    PairString[] p  =
 4 ExpandedBlockStart.gifContractedBlock.gif      dot.gif {
 5InBlock.gif         new PairString(Environment.NewLine,"<br>"),
 6InBlock.gif         new PairString("    ","&nbsp;&nbsp;&nbsp;&nbsp;"),
 7InBlock.gif         new PairString("'","''",true,false)
 8ExpandedBlockEnd.gif     }
;
 9 None.gif    sk.Init();
10 None.gif    sk.FilterSet  =  p;
11 None.gif    sk.Filter(dot.gifdot.gif);

一般来说,如果待换字符串在10个以下,使用Replace性能较高。10个以上的,批量替换的优势就体现出来了。

转载于:https://www.cnblogs.com/xiaotie/archive/2005/08/15/215160.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值