批量字符串替换程序
很多时候我们需要用到字符串替换程序,比如插入数据库时需要将'替换成''。当只有少数待换字符串时用String.Replace性能很高,但因为其复杂度是O(n),当待换字符串很多的时候,String.Replace的性能就降下来了。前些天写了几个程序,用于批量替换字符串。
问题:批量替换字符串。定义字符串对PairString{OldValue,NewValue}。
OldValue<------>NewValue
称---->为右过滤,简称过滤。<----为左过滤,或反过滤。
给定字符串S,一个PairString数组,用PairString数组过滤S(左、右可设置)。
程序实现:
(1)PairString
(2)采用Replace实现
(3)批量替换。我最先采用Hashtable实现,测试结果发现性能比较低,然后就直接改用数组实现,写一个hash函数,将待换字符串hash到数组。flexIndex是松弛系数。
(4)例子
一般来说,如果待换字符串在10个以下,使用Replace性能较高。10个以上的,批量替换的优势就体现出来了。
问题:批量替换字符串。定义字符串对PairString{OldValue,NewValue}。
OldValue<------>NewValue
称---->为右过滤,简称过滤。<----为左过滤,或反过滤。
给定字符串S,一个PairString数组,用PairString数组过滤S(左、右可设置)。
程序实现:
(1)PairString
1
public struct PairString
2
{
3
public string OldValue;
4
public string NewValue;
5
public bool IsRightFilter;
6
public bool IsLeftFilter;
7![](/Images/OutliningIndicators/InBlock.gif)
8
public PairString(string oldValue, string newValue)
9
{
10
OldValue=oldValue;
11
NewValue=newValue;
12
IsRightFilter=true;
13
IsLeftFilter=true;
14
}
15![](/Images/OutliningIndicators/InBlock.gif)
16
public PairString(string oldValue, string newValue, bool isRightFilter, bool isLeftFilter)
17
{
18
OldValue=oldValue;
19
NewValue=newValue;
20
IsRightFilter=isRightFilter;
21
IsLeftFilter=isLeftFilter;
22
}
23
}
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](/Images/OutliningIndicators/InBlock.gif)
4
![](/Images/OutliningIndicators/InBlock.gif)
5
![](/Images/OutliningIndicators/InBlock.gif)
6
![](/Images/OutliningIndicators/InBlock.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/InBlock.gif)
9
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
23
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
(2)采用Replace实现
1
public class StringFilter
2
{
3
private PairString[] _filterSet=null;
4![](/Images/OutliningIndicators/InBlock.gif)
5
public PairString[] FilterSet
6
{
7
get
8
{
9
return _filterSet;
10
}
11
set
12
{
13
_filterSet=value;
14
}
15
}
16![](/Images/OutliningIndicators/InBlock.gif)
17![](/Images/OutliningIndicators/InBlock.gif)
18
public StringFilter()
19
{
20
}
21![](/Images/OutliningIndicators/InBlock.gif)
22![](/Images/OutliningIndicators/InBlock.gif)
23
public virtual string Filter(string input)
24
{
25
foreach (PairString p in FilterSet)
26
{
27
if(p.IsRightFilter==true) input=input.Replace(p.OldValue,p.NewValue);
28
}
29
return input;
30
}
31![](/Images/OutliningIndicators/InBlock.gif)
32
public virtual string AntiFilter(string input)
33
{
34
foreach (PairString p in FilterSet)
35
{
36
if(p.IsLeftFilter==true) input=input.Replace(p.NewValue,p.OldValue);
37
}
38
return input;
39
}
40
}
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](/Images/OutliningIndicators/InBlock.gif)
4
![](/Images/OutliningIndicators/InBlock.gif)
5
![](/Images/OutliningIndicators/InBlock.gif)
6
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
15
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
20
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
31
![](/Images/OutliningIndicators/InBlock.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
36
![](/Images/OutliningIndicators/InBlock.gif)
37
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
38
![](/Images/OutliningIndicators/InBlock.gif)
39
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
40
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
(3)批量替换。我最先采用Hashtable实现,测试结果发现性能比较低,然后就直接改用数组实现,写一个hash函数,将待换字符串hash到数组。flexIndex是松弛系数。
1
public class StringBatchFilter:StringFilter
2
{
3
private int width=0;
4
const int flexIndex = 10;
5![](/Images/OutliningIndicators/InBlock.gif)
6
private object[] _rightArray;
7
private object[] _leftArray;
8![](/Images/OutliningIndicators/InBlock.gif)
9
public object[] RightArray
10
{
11
get
12
{
13
return _rightArray;
14
}
15
set
16
{
17
_rightArray = value;
18
}
19
}
20![](/Images/OutliningIndicators/InBlock.gif)
21
public object[] LeftArray
22
{
23
get
24
{
25
return _leftArray;
26
}
27
set
28
{
29
_leftArray = value;
30
}
31
}
32![](/Images/OutliningIndicators/InBlock.gif)
33
public StringBatchFilter()
34
{
35
}
36![](/Images/OutliningIndicators/InBlock.gif)
37
public void Init()
38
{
39
width=flexIndex*FilterSet.Length;
40
RightArray = new object[width];
41
LeftArray = new object[width];
42![](/Images/OutliningIndicators/InBlock.gif)
43
foreach (PairString p in FilterSet)
44
{
45
if(p.IsRightFilter==true)
46
{
47
if(RightArray[p.OldValue[0]%width]==null)
48
{
49
RightArray[p.OldValue[0]%width]=new ArrayList();
50
}
51
((ArrayList)RightArray[p.OldValue[0]%width]).Add(p);
52![](/Images/OutliningIndicators/InBlock.gif)
53
}
54![](/Images/OutliningIndicators/InBlock.gif)
55
if(p.IsLeftFilter==true)
56
{
57
if(LeftArray[p.NewValue[0]%width]==null)
58
{
59
LeftArray[p.NewValue[0]%width]=new ArrayList();
60
}
61
else
62
{
63
((ArrayList)LeftArray[p.NewValue[0]%width]).Add(p);
64
}
65
}
66
}
67
}
68![](/Images/OutliningIndicators/InBlock.gif)
69
public override string Filter(string input)
70
{
71
StringBuilder sb = new StringBuilder((int)(input.Length*1.2));
72![](/Images/OutliningIndicators/InBlock.gif)
73
for (int i=0; i< input.Length; )
74
{
75
int m = input[i]%width;
76
ArrayList l = (ArrayList)RightArray[m];
77
if(l!=null)
78
{
79
for(int j=0;j<l.Count;j++)
80
{
81
PairString p = (PairString)l[j];
82
if(((i+p.OldValue.Length)<input.Length))
83
{
84
if(input.Substring(i,p.OldValue.Length)==p.OldValue)
85
{
86
sb.Append(p.NewValue);
87
i+=p.OldValue.Length;
88
goto label;
89
}
90
}
91
}
92
sb.Append(input[i]);
93
i++;
94
label:
95
;
96
}
97
else
98
{
99
sb.Append(input[i]);
100
i++;
101
}
102
}
103
return sb.ToString();
104
}
105![](/Images/OutliningIndicators/InBlock.gif)
106
public override string AntiFilter(string input)
107
{
108
StringBuilder sb = new StringBuilder((int)(input.Length*1.2));
109![](/Images/OutliningIndicators/InBlock.gif)
110
for (int i=0; i< input.Length; )
111
{
112
int m = input[i]%width;
113
ArrayList l = (ArrayList)LeftArray[m];
114
if(l!=null)
115
{
116
for(int j=0;j<l.Count;j++)
117
{
118
PairString p = (PairString)l[j];
119
if(((i+p.NewValue.Length)<input.Length))
120
{
121
if(input.Substring(i,p.NewValue.Length)==p.NewValue)
122
{
123
sb.Append(p.OldValue);
124
i+=p.NewValue.Length;
125
goto label;
126
}
127
}
128
}
129
sb.Append(input[i]);
130
i++;
131
label:
132
;
133
}
134
else
135
{
136
sb.Append(input[i]);
137
i++;
138
}
139
}
140
return sb.ToString();
141
}
142
}
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](/Images/OutliningIndicators/InBlock.gif)
4
![](/Images/OutliningIndicators/InBlock.gif)
5
![](/Images/OutliningIndicators/InBlock.gif)
6
![](/Images/OutliningIndicators/InBlock.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/InBlock.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
19
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
31
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
35
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
36
![](/Images/OutliningIndicators/InBlock.gif)
37
![](/Images/OutliningIndicators/InBlock.gif)
38
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
39
![](/Images/OutliningIndicators/InBlock.gif)
40
![](/Images/OutliningIndicators/InBlock.gif)
41
![](/Images/OutliningIndicators/InBlock.gif)
42
![](/Images/OutliningIndicators/InBlock.gif)
43
![](/Images/OutliningIndicators/InBlock.gif)
44
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
45
![](/Images/OutliningIndicators/InBlock.gif)
46
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
47
![](/Images/OutliningIndicators/InBlock.gif)
48
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
49
![](/Images/OutliningIndicators/InBlock.gif)
50
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
51
![](/Images/OutliningIndicators/InBlock.gif)
52
![](/Images/OutliningIndicators/InBlock.gif)
53
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
54
![](/Images/OutliningIndicators/InBlock.gif)
55
![](/Images/OutliningIndicators/InBlock.gif)
56
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
57
![](/Images/OutliningIndicators/InBlock.gif)
58
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
59
![](/Images/OutliningIndicators/InBlock.gif)
60
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
61
![](/Images/OutliningIndicators/InBlock.gif)
62
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
63
![](/Images/OutliningIndicators/InBlock.gif)
64
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
65
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
66
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
67
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
68
![](/Images/OutliningIndicators/InBlock.gif)
69
![](/Images/OutliningIndicators/InBlock.gif)
70
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
71
![](/Images/OutliningIndicators/InBlock.gif)
72
![](/Images/OutliningIndicators/InBlock.gif)
73
![](/Images/OutliningIndicators/InBlock.gif)
74
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
75
![](/Images/OutliningIndicators/InBlock.gif)
76
![](/Images/OutliningIndicators/InBlock.gif)
77
![](/Images/OutliningIndicators/InBlock.gif)
78
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
79
![](/Images/OutliningIndicators/InBlock.gif)
80
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
81
![](/Images/OutliningIndicators/InBlock.gif)
82
![](/Images/OutliningIndicators/InBlock.gif)
83
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
84
![](/Images/OutliningIndicators/InBlock.gif)
85
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
86
![](/Images/OutliningIndicators/InBlock.gif)
87
![](/Images/OutliningIndicators/InBlock.gif)
88
![](/Images/OutliningIndicators/InBlock.gif)
89
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
90
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
91
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
92
![](/Images/OutliningIndicators/InBlock.gif)
93
![](/Images/OutliningIndicators/InBlock.gif)
94
![](/Images/OutliningIndicators/InBlock.gif)
95
![](/Images/OutliningIndicators/InBlock.gif)
96
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
97
![](/Images/OutliningIndicators/InBlock.gif)
98
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
99
![](/Images/OutliningIndicators/InBlock.gif)
100
![](/Images/OutliningIndicators/InBlock.gif)
101
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
102
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
103
![](/Images/OutliningIndicators/InBlock.gif)
104
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
105
![](/Images/OutliningIndicators/InBlock.gif)
106
![](/Images/OutliningIndicators/InBlock.gif)
107
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
108
![](/Images/OutliningIndicators/InBlock.gif)
109
![](/Images/OutliningIndicators/InBlock.gif)
110
![](/Images/OutliningIndicators/InBlock.gif)
111
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
112
![](/Images/OutliningIndicators/InBlock.gif)
113
![](/Images/OutliningIndicators/InBlock.gif)
114
![](/Images/OutliningIndicators/InBlock.gif)
115
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
116
![](/Images/OutliningIndicators/InBlock.gif)
117
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
118
![](/Images/OutliningIndicators/InBlock.gif)
119
![](/Images/OutliningIndicators/InBlock.gif)
120
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
121
![](/Images/OutliningIndicators/InBlock.gif)
122
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
123
![](/Images/OutliningIndicators/InBlock.gif)
124
![](/Images/OutliningIndicators/InBlock.gif)
125
![](/Images/OutliningIndicators/InBlock.gif)
126
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
127
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
128
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
129
![](/Images/OutliningIndicators/InBlock.gif)
130
![](/Images/OutliningIndicators/InBlock.gif)
131
![](/Images/OutliningIndicators/InBlock.gif)
132
![](/Images/OutliningIndicators/InBlock.gif)
133
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
134
![](/Images/OutliningIndicators/InBlock.gif)
135
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
136
![](/Images/OutliningIndicators/InBlock.gif)
137
![](/Images/OutliningIndicators/InBlock.gif)
138
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
139
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
140
![](/Images/OutliningIndicators/InBlock.gif)
141
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
142
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
(4)例子
1
StringBatchFilter sk = new StringBatchFilter();
2![](/Images/OutliningIndicators/None.gif)
3
PairString[] p =
4
{
5
new PairString(Environment.NewLine,"<br>"),
6
new PairString(" "," "),
7
new PairString("'","''",true,false)
8
};
9
sk.Init();
10
sk.FilterSet = p;
11
sk.Filter(![](https://www.cnblogs.com/Images/dot.gif)
);
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
5
![](/Images/OutliningIndicators/InBlock.gif)
6
![](/Images/OutliningIndicators/InBlock.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
9
![](/Images/OutliningIndicators/None.gif)
10
![](/Images/OutliningIndicators/None.gif)
11
![](/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
一般来说,如果待换字符串在10个以下,使用Replace性能较高。10个以上的,批量替换的优势就体现出来了。
版权所有,欢迎转载