StringBuilder substring

ukimiku

Active Member
Licensed User
Longtime User
I am using a StringBuilder object and would like to get (read, not remove) a couple of characters from it. However, StringBuilders do not support methods like substring2(). I know that I can convert a StringBuilder object with ToString, but that may be expensive (in terms of time and memory), as the StringBuilder object may get quite large. Is my assumption correct that in a coding like

B4X:
dim a as string
a = SB.ToString.Substring2(30000,30007)        ' SB is the StringBuilder

the whole SB gets converted to a string before substring copies the relevant characters from it? Or is the compiler "intelligent" and converts only the requested characters (30000 to 30007)?

If not, how to go about reading only a few characters from a StringBuilder without the need to temporarily take away a lot of memory? Maybe I should use something like an array of bytes or chars? But I need the thing to be able to grow without losing information (as the re-dimming of an array would entail).

Thanks.

Regards,
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use Reflection to call StringBuilder.substring:
B4X:
Sub SB_SubString(sb As StringBuilder, start As Int, endIndex As Int) As String
   Dim r As Reflector
   r.Target = sb
   Return r.RunMethod3("substring", start, "java.lang.int", endIndex, "java.lang.int")
End Sub

Calling ToString will generate a new string object from the whole StringBuilder.
 
Upvote 0

ukimiku

Active Member
Licensed User
Longtime User
Very useful, this saves me hours I would have had to spend on inventing complicated storage structures. Thank you.

Now I would like to begin learning in greater depth about the reflection library and its possibilities. It has already grown on me since you have mentioned it so often :) Do you/does anyone here have a pointer to a beginner-level introduction to reflection? I suppose it's a common JAVA feature, since I have seen it used in other programming languages as well (in Frink, for instance), and I will bite the bullet then and learn some more JAVA then as well (my JAVA knowledge is preschool-level at best).

(In a way, having to learn some JAVA so as to become a better B4A programmer seems like having to learn Latin in order to know more about English...)

Regards,
 
Upvote 0

ukimiku

Active Member
Licensed User
Longtime User
Ok, I will try to locate a collection of signatures of java methods then.

By the way, as you are implementing new language features into the 2.20 release: will StringBuilders get a substring2() method?
:)

Regards,
 
Upvote 0

wcieslik

Member
Licensed User
Longtime User
That's neat !
I needed a similar thing and also neede to be able to 'indexOf' as well, so modelling off that I ended up with

B4X:
Sub SB_IndexOf (SB As StringBuilder, substr As String) As Int
Dim r As Reflector
   r.Target = SB
   Return r.RunMethod2("indexOf", substr, "java.lang.String")
End Sub

Watch out for the case sensitivity !!!

Cheers,
Wit C.
 
Upvote 0
Top