Picture
 VB Tricks & Tips
 Strings
 Controls
 Files
 Links
Picture

Visitors:

Picture
 Strings

Code snippets about algorithms that deal with strings (searches, reverses, etc.):


Function StrFix(s As String) _
  As String

  ' The StrFix function converts a
  ' string argument to all uppercase
  ' letters and removes any leading
  ' or trailing blanks. This ensures
  ' reliable comparisons between strings
  ' in the program.
 
  StrFix = UCase(Trim(s))
 
End Function  ' StrFix


Function FixName(first As String, last As String) _
  As String
 
  ' The FixName function returns a name string
  ' in the standard format LASTNAME FIRSTNAME.
 
  FixName = StrFix(last) & " " & StrFix(first)
 
 
End Function  ' FixName

 


Dim NameList() As String  ` NameList is a list of names initialised elsewhere

Private Function SearchList(findName As String) _
  As Long
 
  ' The SearchList function performs a binary
  ' search on the alphabetized name list and
  ' looks for the first instance of the target
  ' name represented by the argument findName.
   
  Dim pos1 As Long, pos2 As Long
  Dim posX As Long, midPos As Long
  Dim listLength As Integer ` listLength contains the length of NameList
 
  listLength = Len(NameList)
  ' Initialize the markers in the list.
  pos1 = 1
  pos2 = listLength
  posX = 0
 
  ' Divide the list in half and focus
  ' the search on the half that will contain
  ' the target name if it exists. Continue
  ' this process until the name is found or
  ' the entire list has been searched.
  Do While pos1 <= pos2 And posX = 0
    midPos = (pos1 + pos2) / 2
    If findName = NameList(midPos) Then
      posX = midPos
    ElseIf findName > NameList(midPos) Then
      pos1 = midPos + 1
    Else
      pos2 = midPos - 1
    End If
  Loop
 
  ' If the name has been found, then
  ' check to see if there are any other
  ' instances of the same name higher
  ' in the list.
  If posX > 1 Then
    Do While NameList(posX) _
        = NameList(posX - 1)
      posX = posX - 1
    Loop
  End If
 
  ' Return the index number of the
  ' first instance of the name in the list.
  SearchForName = posX
     
End Function  ' SearchForName


Dim NameList() As String  ` NameList is a list of names initialised elsewhere

Private Sub SortList()

  ' The SortList procedure alphabetizes (sorts in alphabetical order)
  ' a list of name entries.
  Dim i, j
  Dim temp As String
  Dim listLength As Integer ` listLength contains the length of NameList
 
  listLength = Len(NameList)
  ' Compare each record with each of
  ' the records below it.
  For i = 1 To listLength - 1
    For j = i + 1 To listLength
     
      ' Check to see whether two records
      ' are currently out of order.
      If NameList(i) > _
          NameList(j) Then
       
        ' If they are, swap their positions
        ' in the list.
        temp = NameList(i)
        NameList(i) = NameList(j)
        NameList(j) = temp
      End If
    Next j
  Next i
 
End Sub  ' SortList
 


Private Function YesNo(which As Boolean) As String

  ' The YesNo function converts a Boolean value
  ' into a string value of "Yes" or "No."
 
  If which Then
    YesNo = "Yes"
  Else
    YesNo = "No"
  End If
 
End Function  ' YesNo


Private Function Reverse(s As String) As String

    ` Reverses the string s

    Dim t As String
    Dim i, l As Integer

    t = ””
    l = Len(s)

    For i = l To 1 Step -1
        t = t & Mid(s, i, 1)
    Next i

    Reverse = t

End Function   ` Reverse

[VB Tricks & Tips] [Strings] [Controls] [Files] [Links]

Author: John N. Kostaras       Last modification: 2 May 2002
URL: https://jnkvb.tripod.com/