开发者

How to remove "1" from string if it is last character using vbscript

开发者 https://www.devze.com 2023-04-04 05:59 出处:网络
I have got below code in VBSCript: Function getFaci开发者_运维技巧litiesNotes(objComp) Dim strFacilities

I have got below code in VBSCript:

Function getFaci开发者_运维技巧litiesNotes(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

In above function the code strFacilities = strFacilities & "<li>" & objComp.Fields.Item("Facilities").value(intCount) & "</li>" may have below type of values while doing loop.

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

Now I want to remove "1" if it is the last character in the above strings while it is in loop.

Please suggest!!


How about

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


Method 1)
To do this inline, change the main code like this:

...  
    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  
...  

Method 2)
To do this in Function...
Here is the Function:

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

And change the main code like this:

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

In any of these solutions, if the ".value()" could return an empty string ("") or "Null", then you may have to test for these cases.

0

精彩评论

暂无评论...
验证码 换一张
取 消