substr how to obtain???

cirollo

Active Member
Licensed User
Longtime User
Hi, I'm trying to obtain a substring from a read line of a text file,
this is my code

B4X:
Menu.Show
   Dim SVR,USR,PWD,DBN As String
   'leggo dal file di testo i parametri per comporre la stringa di connessione al db remoto
   If FileExist ("DBConnParam.txt") = True Then
      Msgbox("File Parametri Connessione Trovato!",progname,cMsgboxYesNo,cMsgboxHand)
        FileOpen (c1,"DBConnParam.txt",cRead ,, cASCII)
        r = FileRead (c1)
       Do Until r = EOF
           Select r
            Case SubString (""&r,1,3)= "SVR"
               SVR = SubString (""&r,3,StrLength (r))
            Msgbox (SVR)
             Case SubString (""&r,0,3)= "USR"
                 USR = SubString (""&r,3,StrLength (r))
            Msgbox (USR)
         Case SubString (""&r,0,3)= "PWD"
                PWD = SubString (""&r,3,StrLength (r))
            Msgbox (PWD)
         Case SubString (""&r,0,3)= "DBN"
                DBN = SubString (""&r,3,StrLength (r))
            Msgbox (DBN)
         End Select
         r = FileRead (c1)
      Loop
   FileClose (c1)


         '      sum = sum + r
         '       r = FileRead (c1)
         '      x = FileGet (c1, 3) 'x value will be of the third number stored in the file
      '      Msgbox(x,"My First Program",cMsgboxYesNo,cMsgboxHand)
         'Loop
            'FileClose (c1)
   Else
     Msgbox("File Parametri Connessione Non Trovato!",progname,cMsgboxYesNo,cMsgboxHand)
   End If
End Sub

but I cannot obtain the substring of r

where I'm wrong???
 

mjcoon

Well-Known Member
Licensed User
The right-hand part of a "Case" is supposed to be a constant to be compared with the value of the "Select" variable at run time. From "Help":
Case Value2,Value3,Value4 (True if one of the values matches)

What you have written is a set of boolean expressions. It is possible (if you are a registered user) that a compile will fail, but debugging under the IDE does not notice the deviant syntax.

If you put the SubString() in the Select statement this should work. (Except that your first Case takes a different substring from the others, missing the 1st character, which would be a problem.)

I also do not understand why you write
B4X:
""&r
because concatenating an empty string to the front of "r" should not make any difference.

When you write
but I cannot obtain the substring of r
what symptom are you referring to? (It is always more useful to say what happens rather than what does not :D)

Regards, Mike.
 

cirollo

Active Member
Licensed User
Longtime User
yeah!

I'm also using B4A as Klaus stated.

but this is for a ppc program...

and I'm a registered user on both programming languages ;-)
 

cirollo

Active Member
Licensed User
Longtime User
returning in topic...

@mjcoon

yes, the first case is different because of a test (I don't know that, as for B4A the first character has index 0 or 1)

but the problem is that no msgbox is displayed.....

in the debug i can see the value of r but the substring don't take any char....

regarding ""& you're right, it was just a test, I agree with you it's the same!
 

klaus

Expert
Licensed User
Longtime User
Try this code:
B4X:
Select SubString (""&r,0,3)
Case "SVR"
    SVR = SubString (""&r,3,StrLength (r)-3)
    Msgbox (SVR)
Case "USR"
    USR = SubString (""&r,3,StrLength (r)-3)
    Msgbox (USR)
Case "PWD"
    PWD = SubString (""&r,3,StrLength (r)-3)
    Msgbox (PWD)
Case "DBN"
    DBN = SubString (""&r,3,StrLength (r)-3)
    Msgbox (DBN)
End Select
Best regards.
 
Top