B4J Question [SOLVED] [CLOSE] Repalce string in text file.

MichalK73

Well-Known Member
Licensed User
Longtime User
I have a problem.
When I do a string it works and replaces my text.
However, when I load a longer text in a text file with end of line or tab characters, it does not replace the text.
How do I solve this?

B4X:
Sub replace_into_filetext(input As Map) As String
    Dim tresc As String = File.ReadString(File.DirApp , "clear.txt")

    For i=0 To input.Size-1
        Dim key As String = input.GetKeyAt(i)
        If tresc.Contains(key) Then
            tresc = tresc.Replace(input.GetKeyAt(i), input.GetValueAt(i))
        End If
    Next
    
    Return tresc
End Sub
 
Solution
Ehhh I made a wild error in my code. Key i had in "{ }". From one database pull function I had added curly brackets { }, but did not connect to the others. It wasn't until you hinted at Log(input.GetKeyAt(i)) that showed me my stupid mistake and trusted this earlier version of the function from the database.
B4X:
    Dim tresc As String = File.ReadString(File.DirApp , "clear.txt")
    page.Pause
    For i=0 To input.Size-1
        Dim key As String = $"{${input.GetKeyAt(i)}}"$
        If tresc.Contains(key) Then
            tresc = tresc.Replace(key, input.GetValueAt(i))
        End If
    Next

Topic closed and in the bin.

emexes

Expert
Licensed User
However, when I load a longer text in a text file with end of line or tab characters, it does not replace the text.

Is that because your key text is split across lines or tabs?

What would you have it do if you wanted to replace two words with three, eg "apple banana" with "cat dog elephant" where your file contains "apple<end-of-line>banana"? Whereabouts in the replacement words is the <end-of-line> to go? Between "cat" and "dog"? Or "dog" and "elephant"? Or somewhere else?
 
Upvote 0

MichalK73

Well-Known Member
Licensed User
Longtime User
input as MAP has data
key ( {name} ) -> Value ("Michal")

In the text there is e.g.
My name {name} is known to friends.
After the change you have:
My name Michal is known to friends.

I noticed that if it is a string of text without any characters like newline, tab, it changes, but I have formatted text and then it does not change.
 
Upvote 0

emexes

Expert
Licensed User
Add the highlighted lines, run it on a small file that reproduces the end-of-line/tab issue, and post the log output (into a </> code box).

B4X:
Sub replace_into_filetext(input As Map) As String
    Dim tresc As String = File.ReadString(File.DirApp , "clear.txt")
    log(tresc.length)
    log("[[" & tresc & "]]")
   
    For i=0 To input.Size-1
        Dim key As String = input.GetKeyAt(i)
        If tresc.Contains(key) Then
            log("key = [[" & key & "]]")
            log("value = "[[" & input.GetValueAt(i) & "]]")
            log("before = [[" & tresc & "]]")
            tresc = tresc.Replace(input.GetKeyAt(i), input.GetValueAt(i))
            log("after = "[[" & tresc & "]]")
        End If
    Next
   
    Return tresc
End Sub
 
Upvote 0

emexes

Expert
Licensed User
It just occurred to me that an end-of-line/tab might somehow stop the program from getting inside of the if tresc.Contains(key) block.

If that happens, then add the highlighted line:

B4X:
        Dim key As String = input.GetKeyAt(i)
        log(i & tab & "[[" & key & "]]")
        If tresc.Contains(key) Then
 
Upvote 0

MichalK73

Well-Known Member
Licensed User
Longtime User
Ehhh I made a wild error in my code. Key i had in "{ }". From one database pull function I had added curly brackets { }, but did not connect to the others. It wasn't until you hinted at Log(input.GetKeyAt(i)) that showed me my stupid mistake and trusted this earlier version of the function from the database.
B4X:
    Dim tresc As String = File.ReadString(File.DirApp , "clear.txt")
    page.Pause
    For i=0 To input.Size-1
        Dim key As String = $"{${input.GetKeyAt(i)}}"$
        If tresc.Contains(key) Then
            tresc = tresc.Replace(key, input.GetValueAt(i))
        End If
    Next

Topic closed and in the bin.
 
Upvote 0
Solution

Mahares

Expert
Licensed User
Longtime User
Topic closed and in the bin.
Not so fast. Perhaps for the sake of other members, especially the new ones, you should replace the deprecated keywords with the newer ones. Here is a variant of your code without the deprecation:
B4X:
Dim tresc As String = File.ReadString(File.DirApp , "clear.txt")
    page.Pause
    For Each key As String In input
        If tresc.Contains(key) Then
            tresc = tresc.Replace(key, input.Get(key))
        End If
    Next
 
Upvote 0

MichalK73

Well-Known Member
Licensed User
Longtime User
It won't work for me.
I explained that the 'key' from MAP must be { } therefore I made a mistake and the key in MAP is initially without { }.
Your example will not work for me.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
It won't work for me.
I thought the code you have in post #6 is your correct code, since you stamped it as the solution. All I did is replace the deprecated code from that code and put in the new code in post #8. If the code you have in post #6 us not correct, please post the code that solved the issue.
 
Upvote 0

MichalK73

Well-Known Member
Licensed User
Longtime User
My code is correct for my text from the file. I have described exactly what it is. Your code won't work in my case for my text file.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
@Mahares you are missing the Keys
You are correct. I missed the .keys. That is why when you cannot test the code because there is no sample of the text file available, errors happen. If the OP logged the error he had when he tested with my code, it would have been easy to detect, but all he said was: 'Your code won't work in my case for my text file.'.
Now that we have it all squared away without deprecated code, perhaps the OP can correct the title of his thread. 'Repalce should be 'Replace'
 
Upvote 0

MichalK73

Well-Known Member
Licensed User
Longtime User
'Your code won't work in my case for my text file.'.
Did you read about my problem???
I see no. I explained that in the text file you need to replace the variables in curly brackets, and the KEYS do not have them because they are data from the database !!! That's why your code won't work for me because there is no '{ }', that's why my code has two curly braces.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…