U ukimiku Active Member Licensed User Longtime User Sep 4, 2012 #1 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 Code: Copy 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: Sep 4, 2012
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 Code: Copy 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,
Erel B4X founder Staff member Licensed User Longtime User Sep 5, 2012 #2 You can use Reflection to call StringBuilder.substring: Code: Copy 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
You can use Reflection to call StringBuilder.substring: Code: Copy 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.
U ukimiku Active Member Licensed User Longtime User Sep 5, 2012 #3 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
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,
Erel B4X founder Staff member Licensed User Longtime User Sep 6, 2012 #4 Using Reflection is a bit cumbersome but pretty straightforward (in most cases). You just need to know the signature of the Java method. Upvote 0
Using Reflection is a bit cumbersome but pretty straightforward (in most cases). You just need to know the signature of the Java method.
U ukimiku Active Member Licensed User Longtime User Sep 6, 2012 #5 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
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,
W wcieslik Member Licensed User Longtime User Mar 31, 2013 #7 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 Code: Copy 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
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 Code: Copy 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.