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.
Copying updated assets files (1)
** Activity (main) Create, isFirst = true **
Class not found: leitor.texto.like.speakbutton, trying: leitor.df.texto.speakbutton
** Activity (main) Resume **
** Activity (main) Resume **
Error occurred on line: 419 (Main)
java.lang.StringIndexOutOfBoundsException: length=5532; regionStart=5000; regionLength=1000
at java.lang.String.startEndAndLength(String.java:504)
at java.lang.String.substring(String.java:1333)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:697)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:336)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:246)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:78)
at android.view.View.performClick(View.java:5246)
at android.view.View$PerformClick.run(View.java:21200)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6946)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
** Activity (main) Resume **

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
** Atividade (principal) Pause, UserClosed = false **
** Atividade (principal) Criar, isFirst = true **
Classe não encontrada: leitor.texto.like.speakbutton, tentando: leitor.df.texto.speakbutton
** Atividade Resume (principal) **
Um erro ocorreu na linha: 413 (Principal)
java.lang.StringIndexOutOfBoundsException: Comprimento = 5417; regionStart = 4000; regionLength = -2583
em java.lang.String.startEndAndLength (String.java:504)
em java.lang.String.substring (String.java:1333)
em leitor.df.texto.main._btnspeak_click (main.java:2031)
em java.lang.reflect.Method.invoke (Método Nativo)
em java.lang.reflect.Method.invoke (Method.java:372)
em anywheresoftware.b4a.shell.Shell.runMethod (Shell.java:697)
em anywheresoftware.b4a.shell.Shell.raiseEventImpl (Shell.java:336)
em anywheresoftware.b4a.shell.Shell.raiseEvent (Shell.java:246)
em java.lang.reflect.Method.invoke (Método Nativo)
em java.lang.reflect.Method.invoke (Method.java:372)
em anywheresoftware.b4a.ShellBA.raiseEvent2 (ShellBA.java:134)
em anywheresoftware.b4a.BA.raiseEvent2 (BA.java:157)
em anywheresoftware.b4a.BA.raiseEvent (BA.java:153)
em anywheresoftware.b4a.objects.ViewWrapper $ 1.onClick (ViewWrapper.java:78)
em android.view.View.performClick (View.java:5246)
em android.view.View $ PerformClick.run (View.java:21200)
em android.os.Handler.handleCallback (Handler.java:739)
em android.os.Handler.dispatchMessage (Handler.java:95)
em android.os.Looper.loop (Looper.java:145)
em android.app.ActivityThread.main (ActivityThread.java:6946)
em java.lang.reflect.Method.invoke (Método Nativo)
em java.lang.reflect.Method.invoke (Method.java:372)
em com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:1404)
em com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1199)
** Atividade Resume (principal) **
[/ QUOTE]


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
Top