Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vbscript

I have got below code in 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

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

like image 638
Manoj Singh Avatar asked Sep 11 '11 10:09

Manoj Singh


People also ask

How do you remove the last value from a string?

The easiest way is to use the built-in substring() method of the String class. In order to remove the last character of a given String, we have to use two parameters: 0 as the starting index, and the index of the penultimate character.

How do you remove the first and last character of a string?

The idea is to use the deleteCharAt() method of StringBuilder class to remove first and the last character of a string. The deleteCharAt() method accepts a parameter as an index of the character you want to remove.

How can I remove last character from a string in VB net?

In c# or vb.net we can easily remove last character from string by using Remove or Trim or IndexOf properties.

How do I find a character in a string in VBScript?

The InStrRev function returns the position of the first occurrence of one string within another. The search begins from the end of string, but the position returned counts from the beginning of the string. The InStrRev function can return the following values: If string1 is "" - InStrRev returns 0.


2 Answers

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
like image 85
David Avatar answered Nov 15 '22 12:11

David


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.

like image 26
Kevin Fegan Avatar answered Nov 15 '22 12:11

Kevin Fegan