sscanf function in VB?

Is there a function in VB that is simmilar to the C function sscanf?
hubermarAsked:
Who is Participating?
 
deightonConnect With a Mentor progCommented:
There is no directly corresponding function but you can manipulate a string and convert text to numbers. If your string was of the form "FRED 1234 XYZ". you could write a sub such as

sub(text1 as string,numeric1 as double,text2 as string,target as string)
dim i as integer
dim length as integer
dim text3 as string

i = 1
length = len(target)

do until i > length or target(i) = " "
    text1 = text1 + target(i)
loop

i = i + 1

do until i > length or target(i) = " "
    text3 = text3 + target(i)
loop
numeric1 = val(text3)

i = i+1
     
do until i > length or target(i) = " "
    text2 = text2 + target(i)
loop
 
end sub
     
You'd have to tailor individual functions for individual circumstances - You could probably use variants for the variables and then select appropriate translation by 'chopping up'  a format string in the form "%d %s %d" etc

0
 
bin_huwairibCommented:
No huberma, there is no function in VB is similar to sscanf, and I didn't find any API function equivalent to it, but you can use the normal string manipulation functions instead (like InStr, Mid, Left...).

Bin Huwairib
0
 
deightonprogCommented:
CORRECTION TO STRING MANIPULATION

    sub(text1 as string,numeric1 as double,text2 as string,target as string)
    dim i as integer
    dim length as integer
    dim text3 as string

    i = 1
    length = len(target)

    do until i > length or mid(target,i,1) = " "
        text1 = text1 + mid(target,i,1)
    loop

    i = i + 1

    do until i > length or mid(target,i,1) = " "
        text3 = text3 + mid(target,i,1)
    loop
    numeric1 = val(text3)

    i = i+1
         
    do until i > length or mid(target,i,1) = " "
        text2 = text2 + mid(target,i,1)
    loop
     
    end sub
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
hubermarAuthor Commented:
deighton,

That's the method that I'm currently using.  It's a real pain.  I appreciate your suggestion.

Thanks,
Mark
0
 
deightonprogCommented:
What are you trying to do exactly?  I can think of ways of improving my function.
0
 
deightonprogCommented:
'Here's a more complcated example - hope it helps


Sub sscanf(ByVal sBuff As String, ByVal sFormat As String, Optional a, Optional b, Optional c, Optional d)

'Attempt to bring Sscanf function into VB
'uses optional variant parameters
'MAX four parameters in scanf string - but this COULD BE INCREASED

'EXAMPLE sscanf("1234 1.2 6","%i %d %i",a,b,c)
' puts 1234 in a (a is integer)
'        1.2 in b (d is a double)
'       6   in c (c is an integer)

Dim sTemp As String
Dim sID As String
Dim i As Integer
Dim i2 As Integer

'Extract paramater a

Call strip(sBuff, i, sTemp)
Call strip(sFormat, i2, sID)
Call value(sTemp, sID, a)

If IsMissing(b) Then Exit Sub

sBuff = Right(sBuff, Len(sBuff) - i - 1)
sFormat = Right(sFormat, Len(sFormat) - i2 - 1)

'extract parameter b

Call strip(sBuff, i, sTemp)
Call strip(sFormat, i2, sID)
Call value(sTemp, sID, b)

If IsMissing(c) Then Exit Sub

'You would repeat the sections to add new parameters e,f,g,... and put them in the parameter list


sBuff = Right(sBuff, Len(sBuff) - i - 1)
sFormat = Right(sFormat, Len(sFormat) - i2 - 1)

'extract parameter c

Call strip(sBuff, i, sTemp)
Call strip(sFormat, i2, sID)
Call value(sTemp, sID, c)

If IsMissing(d) Then Exit Sub

sBuff = Right(sBuff, Len(sBuff) - i - 1)
sID = Right(sFormat, Len(sFormat) - i2 - 1)

Call strip(sBuff, i, sTemp)
Call strip(sFormat, i2, sID)
Call value(sTemp, sID, d)

End Sub

Sub strip(sBuff As String, iReturn As Integer, sStrip As String)
    'Strip the next value or control code out of the string

    Dim i As Integer

    i = 1
    Do Until i > Len(sBuff) Or Mid(sBuff, i, 1) = " "
        i = i + 1
    Loop
   
    sStrip = Left(sBuff, i - 1)
    iReturn = i - 1

End Sub


Sub value(sExtract As String, sFormat As String, vResult)
   
    If sFormat = "%i" Then
        vResult = Int(Val(sExtract))
    ElseIf sFormat = "%s" Then
        vResult = sExtract
    ElseIf sFormat = "%d" Then
        vResult = Val(sExtract)
    End If
 
End Sub


'EXAMPLE

Private Sub Command1_Click()

    'example function call

    Dim i As Integer
    Dim s As String
    Dim d As Single
    Dim d2 As Double
    Call sscanf("1234 FRED 1592654 1", "%i %s %d %d", i, s, d, d2)
    MsgBox i
    MsgBox s
    MsgBox d
    MsgBox d2
End Sub

0
 
hubermarAuthor Commented:
deighton,

Very impressive.  I thank you for your help.  I will include this in my code.  I wasn't expecting so much help.  You went the extra mile to answer my question.  

Is it permissible to notify you when I ask questions in the future?  

Thanks again,
Mark
0
 
deightonprogCommented:
E-Mail

DEIGHA@GUSCO.COM
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.