Android Question Special characters in strings and LastIndeOf function

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi
I have to read and process some strings, written on an Ascii file. The string is in the form:

xxx yyy zzz number

To separate the "number", I am using LastIndexOf(" "), because the number is after the last space (of course I have trimmed the string after reading and before processing it).
I had the bad surprise to see that this function fails when inside the string I have special characters, but, Unluckily, not all of them, and also the failure is nonsense.
For example the string:

s="Alfaquiques/Sacário 20"

is processed correctly, while for the string:

s="Ribeira do Falcão 34"

LastIndexOf(s) returns 10 which is not correct. Actually 10 is nonsense and maybe the problem is not related to the presence special characters and I have hidden characters (the file was copied from a computer).

Any idea?

Thanks in advance
 

DonManfred

Expert
Licensed User
Longtime User
s="Ribeira do Falcão 34"

LastIndexOf(s)
You are searching a String in a List???

If not then i suggest to give us more and detailed infos on how we could help here. Post a valid code to reproduce the problem.
 
Last edited:
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Seems to work without a problem from the strings copied from your post:

B4X:
    Dim S As String = "Alfaquiques/Sacário 20"
    Log("Len " & S.Length)
    Log("Pos " & S.LastIndexOf(" "))
    Log("Result :" &S.SubString(S.LastIndexOf(" ") + 1))
    S = "Ribeira do Falcão 34"
    Log("Len " & S.Length)
    Log("Pos " & S.LastIndexOf(" "))
    Log("Result :" &S.SubString(S.LastIndexOf(" ") + 1))

** Activity (main) Pause, UserClosed = true **
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Len 22
Pos 19
Result :20
Len 20
Pos 17
Result :34
** Activity (main) Resume **

Check the reported length of the actual strings used, and their content:

B4X:
    For i = 0 To S.Length - 1
        Log(i & " " & S.CharAt(i) & " " & Asc(S.CharAt(i)))
    Next

And you should find any characters that are causing a problem.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi
Thanks to all for replies. I attach the file that I am reading. The code that I use is the following: (sorry, I don't remember how to insert code in message). Moreover this is not working code, but just the involved parts. This should clarify what I mean. By the way I also do a thing on which I am not sure, despite it is not related to the issue. I should open another thread: I start declaring global variable arrays with no size, and I give them a dimension during reading the file.. I am used to work in C/C++ and I don't know exactly how B4A works on this.. but, as I said, this is another question. Thanks for now.


Sub Process_Globals

Type Pozzetto( Nome As String, _
p As Double, _ ' progressiva
x As Double, _
y As Double, _
z As Double, _
q1 As Double, _
q2 As Double, _
d As Float, _
h1 As Float, _
h2 As Float, _
hf As Float, _
hs As Float, _
desc As String)

Type TipoBase( Nome As String, Codice As Short)

End Sub

Sub Globals

Dim TipPoz() As TipoBase
Dim Reti() As TipoBase
Dim FonInfo() As TipoBase
Dim Sistemi() As TipoBase' sistema
Dim MatCol() As TipoBase ' materiale collettore
Dim TipSec() As TipoBase ' tipo sezione
Dim TipCop() As TipoBase ' tipo cobertura
Dim ModIns() As TipoBase ' modo inserção
Dim TipQue() As TipoBase ' tipo queda
Dim DisAcc() As TipoBase ' dispositivos acesso o Tipo Acesso??
Dim FabFec() As TipoBase ' fabricates fecho
Dim Classe() As TipoBase ' classe fecho
Dim ForFec() As TipoBase ' forma fecho
Dim Fregue() As TipoBase ' freguesia !!! sommare 1111 ossia 01 diviene 111101 e 27 -->> 111127
Dim TipRam() As TipoBase ' tipo ramal
Dim EntPro() As TipoBase ' entitá promotrice
Dim Respon() As TipoBase ' Responsabile
Dim Diamet() As TipoBase ' diametri
Dim MatFec() As TipoBase ' material fecho
Dim MatSat() As TipoBase
End Sub

Sub LeggeCategorieIdraulica
If File.Exists(File.DirRootExternal, "FolderWhereTheFileIs/ListaCaixaN_UTF8.txt") Then
Dim TextReader1 As TextReader

TextReader1.Initialize(File.OpenInput(File.DirRootExternal, "FolderWhereTheFileIs//ListaCaixaN_UTF8.txt"))

LeggeTipi(TextReader1,TipPoz)
LeggeTipi(TextReader1,Reti)
LeggeTipi(TextReader1,FonInfo)
LeggeTipi(TextReader1,Sistemi)
LeggeTipi(TextReader1, MatCol)
LeggeTipi(TextReader1,TipSec)
LeggeTipi(TextReader1,TipCop)
LeggeTipi(TextReader1,ModIns)
LeggeTipi(TextReader1,TipQue)
LeggeTipi(TextReader1, DisAcc)
LeggeTipi(TextReader1, FabFec)
LeggeTipi(TextReader1, Classe)
LeggeTipi(TextReader1, ForFec)
LeggeTipi(TextReader1,Fregue)
LeggeTipi(TextReader1,TipRam)
LeggeTipi(TextReader1,EntPro)
LeggeTipi(TextReader1,Respon)
LeggeTipi(TextReader1,Diamet)
LeggeTipi(TextReader1,MatFec)
LeggeTipi(TextReader1, MatSat)

TextReader1.Close
End If
End Sub

Sub LeggeTipi(TextReader1 As TextReader,Lista() As TipoBase)

Dim i,p,n As Int,s As String,tip As TipoBase

n=TextReader1.ReadLine

Dim Lista(n) As TipoBase

For i=0 To Lista.Length-1
s=TextReader1.ReadLine.Trim

p=s.LastIndexOf(" ")
tip.Initialize
tip.Nome=s.SubString2(0,p-1)
tip.Codice=s.SubString(p-1)
Lista(i)=tip
Next
End Sub
 

Attachments

  • ListaCaixaN_UTF8.txt
    6.7 KB · Views: 288
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
In previous message, program crashes when decoding "Ribeira do Falcão 34", because tip.Codice is not a number, as expected, because of the failure on p=s.LastIndexOf(" ")
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Here is the test project. The file is in my tablet in a folder "Smas", as seen in the code. Thanks.
 

Attachments

  • TestRead.zip
    8.3 KB · Views: 243
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
Going back to post #7... If you examine your text file in a hex dump utility you will see that at this line (Ribeira do Falcão 34) your file format changes. There are no blank characters after the place name; instead your data is padded with tab characters (0x09).
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Just another comment: inside the LeggeTipi I have forgot to take away some dummy code that I am using to investigate the problem. As a matter of fact I detect when the string contains "Falc" and I am planning to see better the situation. For this reason you will find that strange piece of code. To avoid useless discussions, I post here the updated project. Sorry again
 

Attachments

  • TestRead.zip
    8.3 KB · Views: 262
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Brian: yes, this should be the problem.. obviously. I was planning to check with hex editor. Probably the file was manually merged..
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi to all. By the way I solved the problem, substituting the "tab" characters with a space and also finding some bugs in my code. Therefore the project that I sent is not valid. I don't repost it because I think of no interest. Thanks to all.
 
Upvote 0
Top