Android Question string & line in a file text

Uniko Sistemi srl

Active Member
Licensed User
Hello everybody. I wanted to know if there is a function that allows me to search for a specific keyword in the file to find out what line it is on and then read the relative line. i have a file with 1000 lines and wanted to know if i could avoid reading each line with a loop and if there was a direct command
thank you all
 
Solution
You already got the answer here: https://www.b4x.com/android/forum/threads/find-a-word-in-to-txt.143799/
Now, as you want to search for a specific line you need to iterate trough all lines to find the machting one.
B4X:
' Read all lines
Dim l as List = File.ReadList(dir,filename)
' Now Iterate through...

DonManfred

Expert
Licensed User
Longtime User
You already got the answer here: https://www.b4x.com/android/forum/threads/find-a-word-in-to-txt.143799/
Now, as you want to search for a specific line you need to iterate trough all lines to find the machting one.
B4X:
' Read all lines
Dim l as List = File.ReadList(dir,filename)
' Now Iterate through the List to find the matching line... See Collections Tutorial
 
Last edited:
Upvote 1
Solution

emexes

Expert
Licensed User

If you have the file loaded as a string, then you can use .IndexOf to find the specific keyword, and then search backwards until you find the end of the preceding line (or start of the file). You won't know the line number, but you will have the whole line (per "read the relative line").

But if you already have the file in memory as a string, then is still probably simpler to split that string into lines and then search the lines.
 
Upvote 0

Uniko Sistemi srl

Active Member
Licensed User
thanks for the reply indeed.
I need because scrolling 1000 lines becomes slow.
can I kindly ask you how I use index of and read the relative line? it can be done ?
thank you very much you are very kind
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Several things you did not include in your thread request. Some times it helps if you include a copy of your text file and attach here:
1. What if there is more than one word you are searching for in one line?
2. What if there are more than one intance of the same string in several lines?
3. Is your search case sensitive or both?
4. What if the number of lines can eventually be in the thousands?
Normally, when you are dealing with a search, you are better off to involve a small SQLite database, but maybe an overkill in you case.
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
Welcome to B4X. It is very fast.
On my computer in Release mode, it took 28 milliseconds to do a linear search for one word in 154000 lines of text.

B4X:
    Dim lines As List = File.ReadList(File.DirAssets, "colors.txt")
    Log(lines.Size)     '154 lines
    Dim marktime As Long = DateTime.Now
    For k = 1 To 1000    'do it 1000 times
        For i = 0 To lines.Size - 1
            Dim s As String = lines.get(i)
            If s.Contains("Crimson") Then Log(i & TAB  & s)
        Next
    Next
    Log(DateTime.Now - marktime)   'Searching 154000 lines took 28 milliseconds, even with logging the 2000 results
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
On my computer in Release mode, it took 28 milliseconds to do a linear search for one word in 154000 lines of text.

I'm guessing the lines are short, like one color per line? Lines shorter than 7 characters don't even need to be searched. And lines longer don't need much searching either, eg a 10 character line only needs to check for a "C" in the first four characters.

Still pretty quick, tho. ?
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
@emexes I know that this going too far, but it may be interesting to the OP and others.

I made the lines longer, about 220 characters each. Only two lines out of 154 contain Crimson.
Curiously, when I ran this again, the time was even shorter, 19 milliseconds. Probably because of runtime optimization of Java code.
Also, I am logging only the first iteration - only two lines are logged.

The main message I want to convey is that it is extremely easy and fast to check out ideas and scenarios in B4X with just a few lines of code.
Anyone who uses B4X already knows that.

B4X:
    Dim lines As List = File.ReadList(File.DirAssets, "colors.txt")
    Log(lines.Size)                                'Log: 154
    For i = 0 To lines.size - 1
        Dim line As String = lines.Get(i)
        Dim sb As StringBuilder: sb.Initialize
        For j = 0 To 9
            sb.Append(line).Append(" ")
        Next
        lines.Set(i, sb.ToString)
    Next
    Log(lines.Get(0).As(String).Length)            'Log: 220
    Dim marktime As Long = DateTime.Now
    For k = 1 To 1000
        For i = 0 To lines.Size - 1
            Dim s As String = lines.get(i)
            If s.Contains("Crimson") Then
                If k = 1 Then Log(i & TAB  & s)
            End If
        Next
    Next
    Log(DateTime.Now - marktime)   'Searching 154000 lines took 19 milliseconds
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
@emexes I know that this going too far

? re: the implication that I don't, especially given that I've just been off looking for ARM string processing instructions...

Curiously, when I ran this again, the time was even shorter, 19 milliseconds. Probably because of runtime optimization of Java code.

Searching 1.7 billion characters per second is plausible, if the CPU has a search mechanism in hardware. I have a vague recollection of some DMA controllers having a stop-on-value mode that could be co-opted into searching for a value.

Or... there is a way to search strings but only have to mostly check every [pattern-length] character, eg every 7th character for Crimson. But it takes a bit of initial setup. On the other hand, that would (sort-of almost) explain the shorter time. ?
 
Upvote 0

emexes

Expert
Licensed User
Searching 1.7 billion characters per second is plausible

I just realised that, for lines that contain the string being searched for, .Contains only needs to search as far as the first occurance of the string on that line.

On the other hand, if only a few (or one?) of the 154 lines contain the string being searched for, then this isn't a massive saving. ?
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
There were only 2 hits in in the file out of 154. However, the reduction in time is not too mysterious since I removed 1992 Logs.
Also, I have found that if you time code in Release mode, timings vary about 5%, depending on initial Java code optimization and garbage collection.
Perhaps we'll carry on this discussion on in another thread. Or not.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…