出于一个数据库的需要,写了一段程序用来处理不同条件下字符串的组合和修改,使用中觉得还算灵活,当然由此想到一些新的需求可以丰富这个程序功能。如下,有兴趣的可以提建议。灵感来自于正则表达式
Function specialchar(source As String, symbol As String, action As String, position As String) As String
'============================================================================
' This program is use to convert some special character
' Programmer: Jacky ***
' action: add / remove
' position: left / right
' symbol: the character you want to handle
' Date: 2008-05-09
' How to use it - example
' specialchar("ABCDEFG","ABC","remove","left") = "DEFG"
' specialchar("ABCD","EFG","add","right") = "ABCDEFG"
'============================================================================
Dim x As Integer
Dim y As Integer
specialchar = source
If action = "add" Then
If position = "left" Then
x = Len(symbol)
If Left(source,x) <> symbol Then
specialchar = symbol & source
End If
Elseif position = "right" Then
x = Len(symbol)
y = Len(source)
If Right(source,x)<>symbol Then
specialchar = source & symbol
End If
End If
End If
If action = "remove" Then
If position = "left" Then
x= Len(symbol)
y = Len(source)
If Left(source,x) = symbol Then
specialchar = Right(source,(y-x))
End If
Elseif position = "right" Then
x = Len(symbol)
y = Len(source)
If Right(source,x) = symbol Then
specialchar = Left(source,(y-x))
End If
End If
End If
End Function
Function specialchar(source As String, symbol As String, action As String, position As String) As String
'============================================================================
' This program is use to convert some special character
' Programmer: Jacky ***
' action: add / remove
' position: left / right
' symbol: the character you want to handle
' Date: 2008-05-09
' How to use it - example
' specialchar("ABCDEFG","ABC","remove","left") = "DEFG"
' specialchar("ABCD","EFG","add","right") = "ABCDEFG"
'============================================================================
Dim x As Integer
Dim y As Integer
specialchar = source
If action = "add" Then
If position = "left" Then
x = Len(symbol)
If Left(source,x) <> symbol Then
specialchar = symbol & source
End If
Elseif position = "right" Then
x = Len(symbol)
y = Len(source)
If Right(source,x)<>symbol Then
specialchar = source & symbol
End If
End If
End If
If action = "remove" Then
If position = "left" Then
x= Len(symbol)
y = Len(source)
If Left(source,x) = symbol Then
specialchar = Right(source,(y-x))
End If
Elseif position = "right" Then
x = Len(symbol)
y = Len(source)
If Right(source,x) = symbol Then
specialchar = Left(source,(y-x))
End If
End If
End If
End Function