Android Question Get Length of Text

Declan

Well-Known Member
Licensed User
Longtime User
I am using the StringFunctions Library (Version 1.05) to get the Length of a Text String as follows:
B4X:
Dim MyZone As String
Dim MyLen As Long

MyZone    = "This is a Test"
MyLen = Len(MyZone)

I have:
B4X:
Sub Process_Globals
       Dim sf As StringFunctions
       sf.Initialize
End Sub

For some reason, the "Len" function is not recognised and is highlighted RED.

I have StringFunctions (version: 1.05) in my Libraries Manager.
 

Declan

Well-Known Member
Licensed User
Longtime User
Yes, MyLen=MyZone.length does work.
However, now I need to filter (parse) this string.
Example:
String is "Zone: 1" through to "Zone: 12"
I need to obtain the numerals (1 to 12)
I can do this with the Mid command:
My_Result = Mid(MyZone, 6, (MyLen - 6)

I would need the StringFunctions Library to accomplish this.
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
Great, thanks, that works very well.
I is great that I do not have to use the StringFunctions Library.
But, when I get into more complex filtering of strings, I could have a problem.
Example:
String = "$R1Z1165200000"
Where:
Field 1 = $
Field 2 = R1
Field 3 = Z1
Field 4 = 165
Field 5 = 200
Field 6 = 0
Field 7 = 0
Field 8 = 0

The StringFunctions Library does have functionality that simplifies life.
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
In B4A the .SubString and .SubString2 methods equate to the string functions of old BASIC.
B4X:
Dim psT as String = "123456789"
' RIGHT function (or MID with no length parameter)
Log(psT.Substring(5))       ' prints "6789"
' MID function (note that the 2nd parameter is the ending index (not inclusive) and not the length of the resulting string)
Log(psT.SubString2(2, 3))   ' prints "3"
Log(psT.SubString2(0, 3))   ' prints "123"
 
Last edited:
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
What functions are you missing ?

For Instance, to filter a string:"$R1Z1165200000"
I would use:
B4X:
Dim TestString As String
Dim MyTemp As String
TestString = "$R1Z1165200000"
MyTemp = Mid(TestString, 5, 3)

Without the StringFunctions Library I don't have this functionality.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I prefer to use as much as possible native functions rather than 'specialized' libraries.
If I remember well, StringFunctions was developped to 'translate' VB functions not implemented directly in B4A but with B4A code and compiled as a library.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
you can also use charAt aswell which might be more handy for the single char values

it is also a core function

B4X:
Field 1 = string.charat(0) '$
Field 2 = string.charat(1&string.charat(2) 'R1
Field 3 = string.charat(3)&string.chartat(4) 'Z1
Field 4 = string.charat(5)&string.chartat(6)&string.charat(7) '165
Field 5 = string.charat(8)&string.chartat(9)&string.chartat(10) '200
Field 6 = string.charat(11) '0
Field 7 = string.charat(12) '0
Field 8 = string.charat(13) '0
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
you can also use charAt aswell which might be more handy for the single char values

it is also a core function

B4X:
Field 1 = string.charat(0) '$
Field 2 = string.charat(1&string.charat(2) 'R1
Field 3 = string.charat(3)&string.chartat(4) 'Z1
Field 4 = string.charat(5)&string.chartat(6)&string.charat(7) '165
Field 5 = string.charat(8)&string.chartat(9)&string.chartat(10) '200
Field 6 = string.charat(11) '0
Field 7 = string.charat(12) '0
Field 8 = string.charat(13) '0
Many thanks
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Don't forget the powerful regex if your strings may have variable length sections!
string1: $1234$56789
String2: $12#3456$789

To extract 1234 from first string and 12 from the second one, regex will be of great help.

udg
 
Upvote 0
Top