Bug? substring2

kohle

Active Member
Licensed User
Longtime User
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
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Check the logs tab:

SS-2014-03-18_15.40.00.png
 

Dim Baznr

Member
Licensed User
Longtime User
For a more coherent SubStringing I use this, that is very similar to the php's substr() behavior:
B4X:
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:
Top