Android Question Splitting CSBuilder text?

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Is it possible to take part of a CSBuilder sequence and maintain the formatting?
Reason I ask is to speed up real time formatting of an SQL EditText. As it is if the text is altered
(say only one character) then the whole text needs to be reformatted. What I would like is for example this:

select field1 from table1

now if we alter this and make it:

select field1 from table2

then only one word needs to be reformatted, table2.

So, what I would like is keep the CSBuilder sequence up to table2 as a CSBuilder variable, reformat table2, so this will be a new variable, then join these 2.

I searched the forum and looked at the methods and properties of CSBuilder, but it doesn't look this is possible. Is this indeed the case or is there a way?

RBS
 

Semen Matusovskiy

Well-Known Member
Licensed User
In general CharSequence (CSBuilder) is almost the same as String.

Yes, such function like B4A Substring will not work, but you can use javaoblect
For example, instead of cs1.Substring (2,5) you can call
B4X:
jo = cs1
cs2  = jo.RunMethod ("subSequence", Array (2, 5))
If you want to combine cs1 and cs2
B4X:
cs.Initialize.Append (cs1).Append (cs2)
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
In general CharSequence (CSBuilder) is almost the same as String.

Yes, such function like B4A Substring will not work, but you can use javaoblect
For example, instead of cs1.Substring (2,5) you can call
B4X:
jo = cs1
cs2  = jo.RunMethod ("subSequence", Array (2, 5))
If you want to combine cs1 and cs2
B4X:
cs.Initialize.Append (cs1).Append (cs2)

Thanks, will try that out.

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
In general CharSequence (CSBuilder) is almost the same as String.

Yes, such function like B4A Substring will not work, but you can use javaoblect
For example, instead of cs1.Substring (2,5) you can call
B4X:
jo = cs1
cs2  = jo.RunMethod ("subSequence", Array (2, 5))
If you want to combine cs1 and cs2
B4X:
cs.Initialize.Append (cs1).Append (cs2)

Got this all working now. It was tricky, but it speeds indeed the SQL formatting up a lot.
Not that it was slow, but possibly it could be a problem on slow phones (although this is
for now a personal app) or with large SQL's.
Thanks again for the tip.

RBS
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Yes, such function like B4A Substring will not work, but you can use javaoblect
Why would you say that. Look at this below example where I use substring2 and substring with csbuilder without the use of JavaObject lib and it works flawlessly. Are you meaning something else with your statement maybe.
B4X:
Dim cs As CSBuilder
    Dim strvar As String ="FRANCE WON THE WORLD CUP IN 2018"
    cs.initialize.Color(Colors.Red).Append(strvar.SubString2(0, 5)).Color(Colors.Blue).Append(strvar.SubString2(5, 8)) _
    .Color(Colors.Green).Append(strvar.SubString(8)).PopAll
    Dim cs1 As CSBuilder
    Dim strvar As String ="  INFORMATIX LOVES IT"
    cs1.initialize.Color(Colors.Magenta).Append(strvar).PopAll
    Dim cs3 As CSBuilder
    cs3.Initialize.Append(cs).Append(cs1)
    lbl.Text=cs3
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
You can use cs.ToString to get a regular string from the styled string.

Yes, but that will change my CSBuilder charsequence to a regular string. I need it to remain charsequence.
Unless I am overlooking something I think I will need the Java object.
This simple function does the job for me:

B4X:
Sub SubCharSequence2(oCS As CSBuilder, iBeginIndex As Int, iEndIndex As Int) As CSBuilder
 
 Dim JObj As JavaObject
 
 JObj = oCS
 
 Return JObj.RunMethod ("subSequence", Array (iBeginIndex, iEndIndex))

End Sub


RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
You are correct.

Thanks for confirming.
Done some further testing and not quite so sure now it is worth it to format only part of the SQL and keep charsequences that remain
unaltered. It does get complex (eg with SQL comments) and gain in time is not that much. Maybe a case of premature optimization, which
as you say is the source of all evil.

RBS
 
Upvote 0
Top