B4J Question Core function in lieu of js.val

bdunkleysmith

Active Member
Licensed User
Longtime User
I am rewriting some code to eliminate use of the jStringFunctions library by using just core functions, but I have hit a snag.

The relevant code is:

B4X:
Dim f As Int = js.Val(js.mid(Text,2,1))

and there does not appear to be an equivalent function for the jStringFunctions "Val".

I thought I'd read that it was not necessary to explicitly convert from string to integer and so tried:

B4X:
Dim f As Int = Text.substring2(1,2)

but if the relevant character in Text = " ", it throws an exception rather than setting f = 0 as happens when using js.val.

Is there a simpler way than having to detect the special case where the relevant character in Text = " ", eg.:

B4X:
Dim f as Int
If Text.substring2(1,2) = " " Then
        f = 0
    Else
        f = Text.substring2(1,2)
End If
 

stevel05

Expert
Licensed User
Longtime User
How about this:

B4X:
Dim F As Int
Dim Text As String = "12345    "
    If IsNumber(Text.substring2(1,2)) Then
        F = Text.substring2(1,2)
    Else
        F = 0
    End If
    Log(F)
 
Upvote 0

bdunkleysmith

Active Member
Licensed User
Longtime User
Thanks steve05 & Erel,

I embarked on this code update because I read your comment Erel "I don't recommend using it as it is not maintained and all its features are already available in the core library." in this post and given that thought there should be an equivalent to js.Val.

Using the IsNumber function (as suggested by steve05) and in a sub (suggested by Erel) effectively achieves that.

Thanks again.
 
Upvote 0
Top