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
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
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?
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
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
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
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
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.
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.
Dim tresc As String = File.ReadString(File.DirApp , "clear.txt")
page.Pause
For Each key As String In input.Keys
If tresc.Contains(key) Then
tresc = tresc.Replace(key, input.Get(key))
End If
Next
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'
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.