高手们,代码不多,但本人不太懂C#的东西,请帮忙修改一下,感谢!private static string ReplaceEx2(string original, string pattern, string replacement)
{
int count, position0, position1;
count = position0 = position1 = 0;
string upperString = original.ToUpper();
string upperPattern = pattern.ToUpper();
int inc = (original.Length/pattern.Length)*(replacement.Length-pattern.Length);
char [] chars = new char[original.Length + Math.Max(0, inc)];
while( (position1 = IndexOf2(upperString, upperPattern, position0)) != -1 )
{
for ( int i=position0 ; i < position1 ; ++i ) chars[count++] = original[i];
for ( int i=0 ; i < replacement.Length ; ++i ) chars[count++] = replacement[i];
position0 = position1+pattern.Length;
}
if ( position0 == 0 ) return original;
for ( int i=position0 ; i < original.Length ; ++i ) chars[count++] = original[i];
return new string(chars, 0, count);
}
private static int IndexOf2(string T, string P, int i)
{
int step = T.Length-P.Length;
for ( ; i <= step ; ++i )
{
for ( int j=0 ; j < P.Length ; ++j )
{
if ( T[i+j] != P[j] )
{
goto LOOP;
}
}
return i;
LOOP:;
}
return -1;
}
{
int count, position0, position1;
count = position0 = position1 = 0;
string upperString = original.ToUpper();
string upperPattern = pattern.ToUpper();
int inc = (original.Length/pattern.Length)*(replacement.Length-pattern.Length);
char [] chars = new char[original.Length + Math.Max(0, inc)];
while( (position1 = IndexOf2(upperString, upperPattern, position0)) != -1 )
{
for ( int i=position0 ; i < position1 ; ++i ) chars[count++] = original[i];
for ( int i=0 ; i < replacement.Length ; ++i ) chars[count++] = replacement[i];
position0 = position1+pattern.Length;
}
if ( position0 == 0 ) return original;
for ( int i=position0 ; i < original.Length ; ++i ) chars[count++] = original[i];
return new string(chars, 0, count);
}
private static int IndexOf2(string T, string P, int i)
{
int step = T.Length-P.Length;
for ( ; i <= step ; ++i )
{
for ( int j=0 ; j < P.Length ; ++j )
{
if ( T[i+j] != P[j] )
{
goto LOOP;
}
}
return i;
LOOP:;
}
return -1;
}
Private Shared Function ReplaceEx2(original As String, pattern As String, replacement As String) As String
Dim count As Integer, position0 As Integer, position1 As Integer
count = InlineAssignHelper(position0, InlineAssignHelper(position1, 0))
Dim upperString As String = original.ToUpper()
Dim upperPattern As String = pattern.ToUpper()
Dim inc As Integer = (original.Length \ pattern.Length) * (replacement.Length - pattern.Length)
Dim chars As Char() = New Char(original.Length + (Math.Max(0, inc) - 1)) {}
While (InlineAssignHelper(position1, IndexOf2(upperString, upperPattern, position0))) <> -1
For i As Integer = position0 To position1 - 1
chars(System.Math.Max(System.Threading.Interlocked.Increment(count),count - 1)) = original(i)
Next
For i As Integer = 0 To replacement.Length - 1
chars(System.Math.Max(System.Threading.Interlocked.Increment(count),count - 1)) = replacement(i)
Next
position0 = position1 + pattern.Length
End While
If position0 = 0 Then
Return original
End If
For i As Integer = position0 To original.Length - 1
chars(System.Math.Max(System.Threading.Interlocked.Increment(count),count - 1)) = original(i)
Next
Return New String(chars, 0, count)
End Function
Private Shared Function IndexOf2(T As String, P As String, i As Integer) As Integer
Dim [step] As Integer = T.Length - P.Length
While i <= [step]
For j As Integer = 0 To P.Length - 1
If T(i + j) <> P(j) Then
GoTo [LOOP]
End If
Next
Return i
[LOOP]:
i += 1
End While
Return -1
End Function