如何删除" 1"来自字符串,如果它是使用vbscript的最后一个字符

时间:2011-09-11 10:07:10

标签: vbscript

我在VBSCript中有以下代码:

Function getFacilitiesNotes(objComp)
            Dim strFacilities
            strFacilities = strFacilities & "<div id =""facilities"">"
            strFacilities = strFacilities & "<ul>"
            For intCount = 1 to objComp.Fields.Item("Facilities").Value.Count
            strFacilities = strFacilities & "<li>" & objComp.Fields.Item("Facilities").value(intCount) & "</li>"
            Next
            strFacilities = strFacilities & "</ul>"
            strFacilities = strFacilities & "</div>"
            getFacilitiesNotes = strFacilities
End Function

在上面的函数中,代码strFacilities = strFacilities & "<li>" & objComp.Fields.Item("Facilities").value(intCount) & "</li>"在执行循环时可能具有以下类型的值。

1)ABC 2)DFG1 3)G231EG 4)REWEREW1 5)DSFWRE3 6)YRTRWER1

现在我想删除“1”,如果它是循环中上述字符串中的最后一个字符。

请建议!!

2 个答案:

答案 0 :(得分:8)

怎么样

Function truncate_one(s)
  If Right(s, 1) = "1" Then 
    truncate_one = Left(s, Len(s) - 1) 
  Else 
    truncate_one = s
  End If
End Function

答案 1 :(得分:1)

方法1)
要内联执行此操作,请更改主代码,如下所示:

...  
    For intCount = 1 to objComp.Fields.Item("Facilities").Value.Count  
        s = objComp.Fields.Item("Facilities").value(intCount)  
        strFacilities = strFacilities & "<li>" & Left(s,Len(s)-1) & Replace(s,"1","",Len(s)) & "</li>"  
    Next  
...  

方法2)
要在功能中执行此操作...
这是功能:

Function RemoveTrailing1(s)  
    RemoveTrailing1 = Left(s, Len(s) - 1) & Replace(s, "1", "", Len(s))  
End Function  

并改变主要代码:

...  
    For intCount = 1 to objComp.Fields.Item("Facilities").Value.Count  
        strFacilities = strFacilities & "<li>" & RemoveTrailing1(objComp.Fields.Item("Facilities").value(intCount)) & "</li>"  
    Next  
...  

在任何这些解决方案中,如果“.value()”可以返回空字符串(“”)或“Null”,那么您可能必须测试这些情况。