B4J Question Confusion about string and conversion

HARRY

Active Member
Licensed User
Hi,

I am pretty new to Basic4Java and I have some basic questions:

1) It is possible to add strings using Str1 + Str2. I know that is cleaner to add integers, but is should work?

2) String1 & String2 is concatination . Right?

3) Dim a as String
Dim b as integer
a=b & cr
a stays empty, where as the value of b is 27 Why?

Harry
 

stevel05

Expert
Licensed User
This works as expected, using extra characters, you can see that the CRLF has been added.

B4X:
    Dim a As String
    Dim b As Int = 27
    a=b & CRLF
  
    Log("*** " & a & " ***")
Yes adding numeric values in strings will work provided they contain only numeric values, otherwise you will get a NumberFormat error.
 

rwblinn

Well-Known Member
Licensed User
Hi,

see example Subs

B4X:
Sub AddStringsAsNumber(Str1 As String, Str2 As String) As Double
    Dim result As Double
    If IsNumber(Str1) AND IsNumber(Str2) Then
        result = Str1 + Str2
    End If
    Return result
End Sub

Sub ConcatenateStrings(Str1 As String, Str2 As String) As String
    Return Str1 & Str2
End Sub

Sub AddCRLF(b As Int) As String
    Return b & CRLF
End Sub

Sub Test
    Log("AddStringsAsNumber: " & AddStringsAsNumber("19", "58"))   'Result = 77
    Log("ConcatenateStrings: " & ConcatenateStrings("19", "58"))         'Result = 1958
    Log("AddCRLF: Line1" & AddCRLF(27) & "Line2")                             'Result Line127Line2 (on next line)
End Sub
 
Top