Android Question StringBuilder libraries

Theera

Expert
Licensed User
Longtime User
Refer to this ,Where can I get StringBuilder libraries for B4A,B4J,and B4i? Ithink B4Aversion13 there isn't thelibrary
 
Last edited:
Solution
Everything is Ok.My problem is solved fromEmexes'scode and Aeric'scode. Let me explain my problem. There are 2 points
of problem. Magret's code, there isn't Split2 and return array of string(her code has only split which return list. so B4A told that itcan't found StringBuilder.jar and another Point, Aeric's example about"\." which is different from VB6.0 coding. The helping of Emexe's code, it can be solved the problem.

aeric

Expert
Licensed User
Longtime User
Yes, I have already created Split2 addition., but I still be occured error.
How do you write it?

Like this?
B4X:
Public Sub Split2 (CurrentString As String, Split_At_Delimiter As String) As String()
    Return Regex.Split(Split_At_Delimiter, CurrentString)
End Sub

B4X:
Dim arrNum() As String = sf.Split2(MyTargetMoney, "\.")
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
I wrote(Imitation of Expert'scode)
B4X:
Public Sub Split2 (CurrentString As String, Split_At_Delimiter As String) As String()
    Dim mystr()= Regex.Split(Split_At_Delimiter, CurrentString)
 Return  mystr
End Sub

But I don't know that using "\."
 
Upvote 0

emexes

Expert
Licensed User
Longtime User
But I don't know that using "\."

Yeah, that's one of the differences between Regex and VB string functions.

Regex uses patterns rather than plain literal strings.

In those patterns, the full stop "." character has a special meaning, just like asterix "*" has special meaning in filenames.

So to use an actual full stop as a delimiter, you need to tell Regex to treat it as a normal full stop character, and not with the special meaning.

To do that, you "escape" the character from the special meaning by putting a backslash "\" in front of it.

The backslash tells Regex to treat the next character just as a normal character, without any special meaning.

Patterns are nice - for example, if you split a date using pattern [\ \-\/\.] then it will handle dates like 25.12.2024 and 24-12-2024 and 25/12/2024 and 25 12 2024.

But they are not quite as simple as plain literal strings. You have to know about escaping certain characters.

I just escape everything that's not alpha-numeric, rather than try remember which characters have special meaning and which don't.
 
Upvote 0

emexes

Expert
Licensed User
Longtime User
This function:
B4X:
Sub Split2(S As String, Delimiter As Char) As String()

    If S.Contains(Delimiter) = False Then
        Return Array As String(S)
    End If
    
    Dim FirstDelimiterAt As Int = S.IndexOf(Delimiter)
    Dim LastDelimiterAt As Int = S.LastIndexOf(Delimiter)
    
    Dim NumFields As Int = 1
    For I = FirstDelimiterAt To LastDelimiterAt
        If S.CharAt(I) = Delimiter Then NumFields = NumFields + 1
    Next
    
    Dim SA(NumFields) As String
    
    Dim FieldNumber As Int = 0
    Dim FieldStart As Int = 0
    
    For I = FirstDelimiterAt To LastDelimiterAt
        If S.CharAt(I) = Delimiter Then
            SA(FieldNumber) = S.SubString2(FieldStart, I)
            FieldNumber = FieldNumber + 1
            FieldStart = I + 1
        End If
    Next
    
    SA(FieldNumber) = S.SubString(FieldStart)
    
    Return SA
    
End Sub
tested by this:
B4X:
Sub AppStart (Args() As String)
    
    Dim TestString() As String = Array As String (    _
        "Now is the time"    ,                    _
        " for all good men " ,                    _
        "to come to the"     ,                    _
        " aid of the party."                    _
    )
    
    For Each S As String In TestString
        Dim Sa() As String = Split2(S, " ")
        
        Log("""" & S & """ split into " & Sa.Length & " substrings:")
        For I = 0 To Sa.Length - 1
            Log(I & TAB & """" & Sa(I) & """")
        Next
    Next
    
End Sub
should produce this
Log output:
Waiting for debugger to connect...
Program started.
"Now is the time" split into 4 substrings:
0    "Now"
1    "is"
2    "the"
3    "time"
" for all good men " split into 6 substrings:
0    ""
1    "for"
2    "all"
3    "good"
4    "men"
5    ""
"to come to the" split into 4 substrings:
0    "to"
1    "come"
2    "to"
3    "the"
" aid of the party." split into 5 substrings:
0    ""
1    "aid"
2    "of"
3    "the"
4    "party."
Program terminated (StartMessageLoop was not called).
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
Is there checking like this (alikeVB6.0 code)
B4X:
If Split_At_Delimiter.contain(special meaning) then
  Split_At_Delimiter="\"+Split_At_Delimiter
End If
 
Upvote 0

emexes

Expert
Licensed User
Longtime User
I wrote(Imitation of Expert'scode)
B4X:
Public Sub Split2 (CurrentString As String, Split_At_Delimiter As String) As String()
    Dim mystr()= Regex.Split(Split_At_Delimiter, CurrentString)
 Return  mystr
End Sub
But I don't know that using "\."

You could do this:
B4X:
Sub EscapeForRegex(S As String) As String
    Dim sb As StringBuilder
    sb.Initialize
  
    For I = 0 to S.Length - 1
        sb.Append("\")
        sb.Append(S.CharAt(I))
    Next
  
    Return sb.ToString
End Sub
B4X:
Public Sub Split2 (CurrentString As String, Split_At_Delimiter As String) As String()
    Return Regex.Split( EscapeForRegex(Split_At_Delimiter), CurrentString )
End Sub

but you still need to keep an eye out for the disappearing trailing empty matches per:

1735301854698.png
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
Longtime User
describe the original problem that you are trying to solve

I might be a bit slow on the uptake here, but better late than never.

Are you creating a library with VB-compatible functions that are not already implemented in (at least) the Core of B4A/B4I/B4J ?

If yes, then: is there not already a library that does that? Or is it missing some functions? Or is it only half-done, as in VB tends to count characters (and arrays) starting from 1 ie the way humans naturally count, whereas B4A and B4J are firmly in the starting from 0 group ie the way computers naturally count.

PowerBasic has some nice string-function enhancements, eg LEFT$("abcdefg", 3) returns the usual 3 left characters "abc" but LEFT$("abcdefg", -3) returns the characters to the left not including the last 3 ie "abcde", equivalent to LEFT$("abcdefg", LEN("abcdefg") - 3). Similarly for RIGHT$, probably MID$ too. TRIM$/LTRIM$/RTRIM$ can specify which characters are to be trimmed (ie not just spaces). CHR$ can do multiple characters eg CHR$(13, 10) is same as CHR$(13) + CHR$(10). ASC can get character at any position in string, like B4X String.CharAt.
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
I might be a bit slow on the uptake here, but better late than never.

Are you creating a library with VB-compatible functions that are not already implemented in (at least) the Core of B4A/B4I/B4J ?

If yes, then: is there not already a library that does that? Or is it missing some functions? Or is it only half-done, as in VB tends to count characters (and arrays) starting from 1 ie the way humans naturally count, whereas B4A and B4J are firmly in the starting from 0 group ie the way computers naturally count.

PowerBasic has some nice string-function enhancements, eg LEFT$("abcdefg", 3) returns the usual 3 left characters "abc" but LEFT$("abcdefg", -3) returns the characters to the left not including the last 3 ie "abcde", equivalent to LEFT$("abcdefg", LEN("abcdefg") - 3). Similarly for RIGHT$, probably MID$ too. TRIM$/LTRIM$/RTRIM$ can specify which characters are to be trimmed (ie not just spaces). CHR$ can do multiple characters eg CHR$(13, 10) is same as CHR$(13) + CHR$(10). ASC can get character at any position in string, like B4X String.CharAt.
Really, B4XStringFunctionVb is from Margret's library(StringFunction2), but she isn't alive here.I think that is useful for non-expert and the man who understand Vb6.0 functions.I believe that the library help to import Vbprogrammers into our B4X family which helps selling B4i product more.As I'm non-expert, I know that the problem in coding ismanaged about string, array, date, etc. Manytimes, I need to use B4XStringFunctionVb forcoding.(wishs B4XStringFunctionVb is the one of internal library for all of non-experts)

Best regards to all of experts
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
Last edited:
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
Ok, now I understood clearly.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
To use B4X, just learn B4X.
The functions are unnecessary added complexity, ugly and waste of time.
If someone insist, I won't stop him.
 
Upvote 0

emexes

Expert
Licensed User
Longtime User
StringFunction2 is supported only B4A andB4J, so I need to be B4X supported as well.

As in: you need StringFunction2 to support B4I as well as B4A and B4J ?

Are you recreating it as a B4XLIB that can support all 3 ?
( because JAR+XML format libraries only work for Java-based B4A and B4J )
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
To use B4X, just learn B4X.
The functions are unnecessary added complexity, ugly and waste of time.
If someone insist, I won't stop him.
I understand your taught,but I think Erel's products are made for all of non-experts order to escape Java coding,Obj-C ,etc,so I think B4XStringfunctionVB should be haved in B4X. Erel can get a lot of new delvelopers who need coding in mobile easily.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I understand your taught,but I think Erel's products are made for all of non-experts order to escape Java coding,Obj-C ,etc,so I think B4XStringfunctionVB should be haved in B4X. Erel can get a lot of new delvelopers who need coding in mobile easily.
I don't think you understand what I wanted to say.
B4X is not VB6!
You can't find a new girlfriend who is exactly the same as the ex girlfriend you have lost. Just appreciate the new girlfriend is way better than your ex. VB6 is a thing in the past and will never evolve. It's time to leave her behind. In fact, I already did it 10 years ago.
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
I don't think you understand what I wanted to say.
B4X is not VB6!
You can't find a new girlfriend who is exactly the same as the ex girlfriend you have lost. Just appreciate the new girlfriend is way better than your ex. VB6 is a thing in the past and will never evolve. It's time to leave her behind. In fact, I already did it 10 years ago.
It is the easiest way to increase the number of Vb programmers to B4X programmers.
Creating a CustomView that is like creating ActiveX I believe that everyone is used to driving a car, but if one day you have to drive a truck, then if the driving design is not the same, you will have to drive a car. It is a waste of time to learn.Creating a CustomView that is like creating ActiveX I believe that everyone is used to driving a car, but if one day you have to drive a truck, then if the driving design is not the same, you will have to drive a car. It is a waste of time to learn quite a lot.
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
Before I came across B4A, I knew Basic4ppc before. I knew Appforge a lot before Appforge was designed like ActiveX. It is called igot. If I remember correctly, it's as easy to use as writing VB6.0, it's a great design for people who write apps on Windowsmobile. This feeling is what I want to see in B4X products. If B4J and B4i Writing is not like B4A, how much hassle will it be in app development?
 
Upvote 0
Top