VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Library of handy functions (string manipulation, algebra, dates)

by C. van Dorsten (1 Submission)
Category: Miscellaneous
Compatability: Visual Basic 5.0
Difficulty: Unknown Difficulty
Originally Published: Sat 7th September 2002
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Library of handy functions (string manipulation, algebra, dates)

API Declarations


Public Function Begins(ByVal strSearch As String, ByVal strSource As String) As Boolean
strSource = LCase(strSource)
strSearch = LCase(strSearch)
Begins = (Mid(strSource, 1, Len(strSearch)) = strSearch Or strSearch = "")
End Function

' Invert a string
Public Function InvertString(ByVal strInvert As String) As String
Dim intCnt As Integer
Dim strHelp As String

intCnt = 1
Do
strHelp = Mid(strInvert, intCnt, 1) + strHelp
intCnt = intCnt + 1
Loop While intCnt <= Len(strInvert)
InvertString = strHelp
End Function

' Reverse Instr: starts search from the end of the base string
Public Function RInStr(ByVal strInStr As String, ByVal strSearch As String) As Integer
Dim s As Integer

RInStr = 0
s = Max(Len(strInStr) - Len(strSearch), 1)
Do While RInStr = 0 And s > 0
RInStr = InStr(s, strInStr, strSearch, 1)
s = s - 1
Loop
End Function

' Pick a substring from a base string based on a separator.
Public Function Entry(ByVal iEntry As Byte, ByVal strEntry As String, Optional ByVal strSep As String)
Dim s As Integer
Dim e As Integer
Dim i As Integer

If strSep = "" Or IsNull(strSep) Then strSep = ";"
iEntry = Min(Max(iEntry, 1), NumEntries(strEntry, strSep))
strEntry = strSep & strEntry & strSep
e = 0
Do While iEntry > 0
e = InStr(e + 1, strEntry, strSep)
iEntry = iEntry - 1
Loop
s = e + 1
e = InStr(e + 1, strEntry, strSep)
e = e - s
Entry = Mid(strEntry, s, e)
End Function

' Determin the number of entries in a base string based on a certain separator. Returns 1 as lower boundary.
Public Function NumEntries(ByVal strEntry As String, Optional ByVal strSep As String) As Integer
Dim intCnt As Integer
Dim iInstr As Integer
Dim Expr As String

If strSep = "" Or IsNull(strSep) Then strSep = ";"
iInstr = InStr(strEntry, strSep)
Do While iInstr > 0
iInstr = InStr(iInstr + 1, strEntry, strSep)
intCnt = intCnt + 1
Loop
NumEntries = intCnt + 1
End Function

' Put as string in (single) quotes.
Public Function Quote(ByVal varSource As Variant, Optional ByVal bteOption As Byte) As Variant
If bteOption = 1 Then
Quote = "'" & varSource & "'"
Else
Quote = """" & varSource & """"
End If
End Function

' Convert a date to a weeknumber
Public Function CWeek(ByVal dtDateVar As Variant) As Integer
Dim dtDate(3) As Date
Dim dtParam As Date
Dim bteWeekNo As Integer
Dim booExit As Boolean
Dim bteAdd As Integer
Dim bteDiv As Byte

bteDiv = 8 ' Change to 7 to make monday the first day of the week
dtParam = dtDateVar
booExit = True
bteAdd = 0
Do
dtDate(0) = "01/01/" & Year(dtParam) + bteAdd ' 1st of Jan.
dtDate(1) = "03/01/" & Year(dtParam) + bteAdd ' 3rd of Jan.
dtDate(2) = "10/01/" & Year(dtParam) + bteAdd ' 10th of Jan.
If WeekDay(dtDate(0)) <= 4 Then
bteWeekNo = Truncate((dtParam - (dtDate(1) - WeekDay(dtDate(0))) + bteDiv) / 7, 0)
Else
bteWeekNo = Truncate((dtParam - (dtDate(2) - WeekDay(dtDate(0))) + bteDiv) / 7, 0)
End If
If booExit And (bteWeekNo < 1 Or bteWeekNo > 52) Then
If bteWeekNo < 1 Then bteAdd = -1
If bteWeekNo > 52 Then bteAdd = 1
booExit = False
Else
booExit = True
End If
Loop Until booExit = True
If bteWeekNo = 0 Then bteWeekNo = 53
CWeek = bteWeekNo
End Function

' Convert a date to a weeknumber based on an offset. Used in financial applications to determin the
' weeknumber in a broken bookyear. Pass the monthnumber of the starting month of a bookyear and
' the date that needs to be converted to a weeknumber.
Public Function CWeekOffset(ByVal intMonth As Integer, ByVal dtDateVar As Variant) As Integer
Dim intWeekOffset As Integer
Dim intWeekAct As Integer
Dim intWeeks As Integer
Dim intOffMonth As Integer
Dim dtParam As Date
Dim dtNewDate As Date

' Initialize parameters and variables
dtParam = dtDateVar
intWeekAct = CWeek(dtParam) ' Calendar week of the date passed to this function
intOffMonth = InitVar(Min(Max(intMonth, 1), intMonth Mod 12), 0, 12)

' Determine the offset week number
If Month(dtParam) >= intOffMonth _
Then dtNewDate = "1/" & intOffMonth & "/" & Year(dtParam) _
Else: dtNewDate = "1/" & intOffMonth & "/" & Year(dtParam) - 1
intWeekOffset = CWeek(dtNewDate) ' Weekoffset of the date in a bookyear, example: 1-feb-2002 in a bookyear that
' runs from feb. - jan. is in week 1 of the bookyear instead of its actual week 5,
' of the calendaryear. Whereas 31-jan-2002 - which is also in week 5 of the
' calendaryear - must lie in the last week of the previous calendar year.

' Determine the number of weeks in a year

Rate Library of handy functions (string manipulation, algebra, dates)




a = "My documents are very valuable to me"
MsgBox Begins(a,"My documents") ' will show "True"
MsgBox InvertString(a)          ' will show "em ot elbaulav yrev era stnemucod yM"
MsgBox RInstr(a,"e")            ' will show 36 whereas
MsgBox Instr(a,"e")             ' will show 9
MsgBox NumEntries(a," ")        ' will show 7
MsgBox Entry(3,a," ")           ' will show "are"


Download this snippet    Add to My Saved Code

Library of handy functions (string manipulation, algebra, dates) Comments

No comments have been posted about Library of handy functions (string manipulation, algebra, dates). Why not be the first to post a comment about Library of handy functions (string manipulation, algebra, dates).

Post your comment

Subject:
Message:
0/1000 characters