K kohle Active Member Licensed User Longtime User Mar 18, 2014 #1 Hi, I downloaded the new version on basic4android and wrote a little program and recognized a bug: Here a demo: dim key as string dim key_field as string key="SMS" key_field = key.SubString2(0,7) So whats happen is, when the string is smaller, than from index to index than the program just finish at the substring2 line. No error message, nothing. In other languagues, like vb6 .... the result would be "SMS" For newcomers to b4a its a real hint, and can take ours of searching. rgs Kohle
Hi, I downloaded the new version on basic4android and wrote a little program and recognized a bug: Here a demo: dim key as string dim key_field as string key="SMS" key_field = key.SubString2(0,7) So whats happen is, when the string is smaller, than from index to index than the program just finish at the substring2 line. No error message, nothing. In other languagues, like vb6 .... the result would be "SMS" For newcomers to b4a its a real hint, and can take ours of searching. rgs Kohle
Dim Baznr Member Licensed User Longtime User Aug 13, 2014 #3 For a more coherent SubStringing I use this, that is very similar to the php's substr() behavior: Code: Copy Sub substr( s As String, i As Int, n As Int ) As String Dim m As Int = s.Length If i>m Then i = i mod m If i<0 Then i = Abs(m+i) mod m If (n<=0) OR (i+n > m) Then n = m-i Return s.SubString2( i, i+n ) End Sub Use: mychunk = substr( mystring, start, len) - if start < 0 then it countsdown from the end of mystring. - if start is over the end of mystring then it recycles from the begining. - if len <= 0 then selects all the chars from start to the end. - if mystring is shorter than len requests, no error occurs, and selects only the available chars. Last edited: Aug 13, 2014
For a more coherent SubStringing I use this, that is very similar to the php's substr() behavior: Code: Copy Sub substr( s As String, i As Int, n As Int ) As String Dim m As Int = s.Length If i>m Then i = i mod m If i<0 Then i = Abs(m+i) mod m If (n<=0) OR (i+n > m) Then n = m-i Return s.SubString2( i, i+n ) End Sub Use: mychunk = substr( mystring, start, len) - if start < 0 then it countsdown from the end of mystring. - if start is over the end of mystring then it recycles from the begining. - if len <= 0 then selects all the chars from start to the end. - if mystring is shorter than len requests, no error occurs, and selects only the available chars.