某些人总要在提示了输入"/"的情况下偏要输入"\"
某些人总要在提示了输入"/"的情况下偏要输入"\\"
某些人总要在提示了输入"/"的情况下偏要输入"//"
为了不被 "我就要!" 这样的话语堵得哑口无言,
这时候,以下函数(getUniformSeparator)怀着十足的使命感默默地跳出来:
' ===========================================
' 将路径字符串当中的\\,\,//转化成统一的/
' 并返回处理后的结果
' ===========================================
' 参数如下:
' sPath :待处理的路径字符串
Function getUniformSeparator(ByVal sPath)
Dim iLen
sPath = Trim(""&sPath)
iLen = Len(sPath)
If iLen > 0 Then
sPath = Replace(sPath, "\", "/") ' 消灭\\和\
Do While Instr(sPath, "//") > 0 '消灭//
sPath = Replace(sPath, "//" , "/")
Loop
End If
getUniformSeparator = sPath
End Function
某些人(包括我)总会轻易间将"以/结尾","以/开头","不要以/结尾","不要以/开头" 的提示语从正眼略到余光再抛之脑后,
并以一句"我哪知!"来诉苦.好吧,以下函数(getFormatePath)会省略NNN遍的"我告诉你......":
' ===========================================
' 将路径字符串进行格式化处理(包括路径分隔符的统一)
' 并返回处理后的结果
' 本函数依赖于:getUniformSeparator函数
' ===========================================
' 参数如下:
' sPath :待处理的路径字符串
' sType :格式化形式,有"L","R","LR","K"等几种形式(不分大小写)
' "L"--"只有左侧有路径符"(left)
' "R"--"只有右侧有路径符"(right)
' "LR"--"左右侧都有路径符"(left&right)
' "K"--"不变化,原来怎么样就怎么样"(keep)
Function getFormatePath(ByVal sPath, ByVal sType)
Dim bLeft,bRight,iLen
sPath = getUniformSeparator(sPath)
sType = UCase(Trim(""&sType))
iLen = Len(sPath)
' 对于根路径,快速处理
If iLen < 2 Then
If sType = "L" Or sType = "R" Or sType = "LR" Then
sPath = "/"
ElseIf sType <> "K" Then
sPath = ""
End If
getFormatePath = sPath
Exit Function
End If
bLeft = (Left(sPath, 1) = "/") '以/开头
bRight = (Right(sPath,1) = "/") '以/结尾
Select Case sType
Case ""
If bLeft Then sPath = Mid(sPath, 2, iLen)
If bRight Then sPath = Mid(sPath, 1, Len(sPath)-1)
Case "L"
If Not bLeft Then sPath = "/" & sPath
If bRight Then sPath = Mid(sPath, 1, Len(sPath)-1)
Case "R"
If bLeft Then sPath = Mid(sPath, 2, iLen-1)
If Not bRight Then sPath = sPath & "/"
Case "LR"
If Not bLeft Then sPath = "/" & sPath
If Not bRight Then sPath = sPath & "/"
Case "K"
' Sth. to do...
Case Else
If bLeft Then sPath = Mid(sPath, 2, iLen)
If bRight Then sPath = Mid(sPath, 1, Len(sPath)-1)
End Select
getFormatePath = sPath
End Function