Advertisement
ASP_Volume2 String Manipulation #31208

ExtractArgument

I use ExtractArgument (written by my friend Mike Carper) all the time. It returns an argument or token from a string based on its position within another string and a delimiter. For example: I want the "2" in the following string: "1,2,3,4,5,6,7,8,9,10". 'Sample call 'Dim sList as string 'Dim sTown as string 'sList = "POB 145,Dexter Street,Anytown,USA" 'sTown = ExtractArgument(3, sList, ",") 'sTown will be "Anytown" I find this very useful in working with delimited files and strings, and have implemented it in INI settings as well.

AI

Podsumowanie AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Kod źródłowy
original-source
Function ExtractArgument (ArgNum As Integer, srchstr As String, Delim As String) As String
  'Extract an argument or token from a string based on its position
  'and a delimiter.
  On Error GoTo Err_ExtractArgument
  Dim ArgCount As Integer
  Dim LastPos As Integer
  Dim Pos As Integer
  Dim Arg As String
  
  Arg = ""
  LastPos = 1
  If ArgNum = 1 Then Arg = srchstr
  
   Do While InStr(srchstr, Delim) > 0
    Pos = InStr(LastPos, srchstr, Delim)
    If Pos = 0 Then
      'No More Args found
      If ArgCount = ArgNum - 1 Then Arg = Mid(srchstr, LastPos)
      Exit Do
    Else
      ArgCount = ArgCount + 1
      If ArgCount = ArgNum Then
        Arg = Mid(srchstr, LastPos, Pos - LastPos)
        Exit Do
      End If
    End If
    LastPos = Pos + 1
  Loop
  
  '---------
  ExtractArgument = Arg
  Exit Function
Err_ExtractArgument:
  MsgBox "Error " & Err & ": " & Error
  Resume Next
End Function


Upload
Oryginalne komentarze (3)
Odzyskane z Wayback Machine