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!!
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.
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.
In c# or vb.net we can easily remove last character from string by using Remove or Trim or IndexOf properties.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With