自编Replace方法

自己编写了一个VB.Net的Replace方法,与大家分享!若有更好的方法,我们可以一起讨论!
Public   Function Replace(ByVal strSource As StringByVal oldStr As StringByVal newStr As StringAs String

    
Dim idxSearch As Integer
    
Dim isExist As Boolean = False
    
Dim strReturn As String = ""

    
For i As Integer = 0 To strSource.Length - 1
        
If AscW(strSource.Chars(i)) = AscW(oldStr.Chars(0)) Then
            
'找到匹配要替换的第一个字符
            isExist = True
            
For j As Integer = 0 To oldStr.Length - 1
                
'继续查找是否存在此oldStr
                If AscW(oldStr.Chars(j)) <> AscW(strSource.Chars(i + j)) Then
                    isExist 
= False
                    
Exit For
                
End If
            
Next j

            
If isExist Then
                
'若strSource存在oldStr,则将newStr接在strReturn后面
                For k As Integer = 0 To newStr.Length - 1
                    strReturn 
&= newStr.Chars(k)
                
Next
            
Else
                strReturn 
&= strSource.Chars(i)
            
End If

        
Else
            strReturn 
&= strSource.Chars(i)
        
End If
    
Next i

    
Return strReturn

End Function


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值