B4R strings are different than in other B4X tools. The reasons for these differences are:
1. Very limited memory.
2. Lack of Unicode encoders.
A String object in B4R is the same as C char* string. It is an array of bytes with an additional zero byte at the end.
The requirement of the last zero byte makes it impossible to create a substring without copying the memory to a new address. For that reason arrays of bytes are preferable over Strings. The various string related methods work with arrays of bytes.
Converting a string to an array of bytes is very simple and doesn't involve any memory copying. The compiler will do it automatically when needed:
Concatenation
The & operator is not available in B4R. If you do need to concatenate strings or arrays of bytes then you can use JoinStrings or JoinBytes keywords. They are more efficient as they concatenate all the elements at once.
In most cases you don't need to concatenate strings. A better solution for example when sending strings (or bytes) with AsyncStreams:
String Methods
The standard string methods are available in ByteConverter type (rRandomAccessFile library).
They are similar to the string methods in other B4X tools:
Note how both strings and array of bytes can be used as the compiler converts strings to arrays of bytes automatically.
With the exception of JoinStrings, none of the above methods make a copy of the original string / bytes.
This means that modifying the returned array as in the last three lines will also modify the original array.
It will also happen with string literals that all share the same memory block:
Encoding
There is no real support for Unicode encodings in Arduino. You are always working with raw bytes.
1. Very limited memory.
2. Lack of Unicode encoders.
A String object in B4R is the same as C char* string. It is an array of bytes with an additional zero byte at the end.
The requirement of the last zero byte makes it impossible to create a substring without copying the memory to a new address. For that reason arrays of bytes are preferable over Strings. The various string related methods work with arrays of bytes.
Converting a string to an array of bytes is very simple and doesn't involve any memory copying. The compiler will do it automatically when needed:
B4X:
Dim b() As Byte = "abc" 'equivalent to Dim b() As Byte = "abc".GetBytes
Concatenation
The & operator is not available in B4R. If you do need to concatenate strings or arrays of bytes then you can use JoinStrings or JoinBytes keywords. They are more efficient as they concatenate all the elements at once.
In most cases you don't need to concatenate strings. A better solution for example when sending strings (or bytes) with AsyncStreams:
B4X:
AStream.Write("The current value is: ").Write(s).Write(" and the other value is: ")
AStream.Write(NumberFormat(d, 0,0))
String Methods
The standard string methods are available in ByteConverter type (rRandomAccessFile library).
They are similar to the string methods in other B4X tools:
B4X:
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
Dim bc As ByteConverter
Log("IndexOf: ", bc.IndexOf("0123456", "3")) 'IndexOf: 3
Dim b() As Byte = " abc,def,ghijkl "
Log("Substring: ", bc.SubString(b, 3)) 'Substring: c,def,ghijkl
Log("Trim: ", bc.Trim(b)) 'Trim: abc,def,ghijkl
For Each s() As Byte In bc.Split(b, ",")
Log("Split: ", s)
'Split: abc
'Split: def
'Split: ghijkl
Next
Dim c As String = JoinStrings(Array As String("Number of millis: ", Millis, CRLF, "Number of micros: ", Micros))
Log("c = ", c)
Dim b() As Byte = bc.SubString2(c, 0, 5)
b(0) = Asc("X")
Log("b = ", b)
Log("c = ", c) 'first character will be X
End Sub
Note how both strings and array of bytes can be used as the compiler converts strings to arrays of bytes automatically.
With the exception of JoinStrings, none of the above methods make a copy of the original string / bytes.
This means that modifying the returned array as in the last three lines will also modify the original array.
It will also happen with string literals that all share the same memory block:
B4X:
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
Dim bc As ByteConverter
Dim b() As Byte = bc.Trim("abcdef ")
b(0) = Asc("M") 'this line will change the value of the literal string
dim s as String = "abcdef "
Log(s) 'Mbcdef
End Sub
Encoding
There is no real support for Unicode encodings in Arduino. You are always working with raw bytes.
B4X:
Dim s As String = "אראל"
Log(s) 'will print correctly
Log(s.Length) '8 because each of the non-ASCII characters in this case takes two bytes
Last edited: