Android Question Logic Question ( split a string)

Douglas Farias

Expert
Licensed User
Longtime User
hi all.
this is a noob question but i think someone can help me *-*.

the tts have the character limit in the function tts.play(string) = 4000 char on some devices
on my app the user can open any text.

some text´s have 4000+.

how you could distribute this text in several strings of 4000 ?

for example the text have 12800 characters, the tts max char is 4000

12800 / 4000 = 3,2

how can i read 3,2 times 4000 characters?
and on the last string this not will have 4000 characters
12800 / 4000

4000
4000
4000
800

how can i make this automatic? the user can open any text, this can have 12800 or + characters.
for now i m using 12800 / 4000 = 3,2 < here as int = 3(this dont read all text)

thx all sorry for the noob question
 

Roycefer

Well-Known Member
Licensed User
Longtime User
B4X:
Dim iterations As Int = Ceil(inString.Length/4000)
For j = 0 to iterations-1
    Dim partialString As String = inString.Substring2(j*4000, (j+1)*4000)
    DoSomethingWith(partialString)
Next
 
Last edited:
Upvote 0

Douglas Farias

Expert
Licensed User
Longtime User
Dim iterations As Int = Ceil(inString.Length/4000)For j = 0to iterations-1Dim partialString AsString = inString.Substring(j*4000, (j+1)*4000)
DoSomethingWith(partialString)Next

thx but if the string dont have 4000?
for example 12800 / 4000 = 3,2

4000
4000
4000
800 < will have problems here no?
 
Upvote 0

Douglas Farias

Expert
Licensed User
Longtime User
thx for the help man!
i will try this today in home, many thx
 
Upvote 0

Douglas Farias

Expert
Licensed User
Longtime User
i tested today and show me this error.

B4X:
Dim iterations As Int = Ceil(s.Length/suportado)
        For j = 0 To iterations-1
            Dim partialString As String = s.Substring2(j*suportado, (j+1)*suportado)
            Log(partialString)
        Next

suportado its a int = 1000

on the logs show the text 1000,1000,1000 etc but crash on the end of text and show this logs, the s its a string with 5513 characters
 
Upvote 0

Douglas Farias

Expert
Licensed User
Longtime User


B4X:
 iterações Dim Int = Ceil (s.length / suportado)
        Para j = 0 a iterações-1
            Dim partialString As String = s.Substring2 (j * suportado, Min ((j + 1) * suportado, s.length-j * suportado))
            Log (partialString)
        Próximo [/ code]

Sorry but dont work again :(
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Try this:
B4X:
    'Build some test data
    Dim I As Int
    Dim SB As StringBuilder
    SB.Initialize
    For I = 0 To 12567
        SB.Append(Chr(Rnd(32,64)))
    Next
    Dim S As String = SB.ToString
  
    Log(S.Length & " Chars generated")
  
  
    'Process the string into 4000 char strings
    Dim Result As List
    Result.Initialize
  
    For I = 0 To S.Length - 4000 Step 4000
        Result.Add(S.SubString2(I,I+4000))  
    Next
  
    'And whatever is left
    Result.Add(S.SubString(I))
  
    'Count the chars to make sure we have them all
    Dim Count As Int = 0
    For Each Str As String In Result
        Count = Count + Str.Length
    Next
  
    Log(Count & " Chars processed")
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
My mistake, make that second argument Min((j+1)*suportado, s.Length). Check out the comments for Substring2(). What is being attempted here is straightforward.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…