A apty Active Member Licensed User Longtime User Feb 27, 2015 #1 I tried the code below to replace a specific string in a text file. I am not getting any error but the replacement is not working. Please advice what i am doing wrong Code: Copy mynewfile=File.ReadString(File.DirDefaultExternal&"/testr/","Mai.txt") mynewfile = mynewfile.Replace ("kliev", "jared") File.WriteString(File.DirDefaultExternal&"/testr/","Mai.txt",mynewfile)
I tried the code below to replace a specific string in a text file. I am not getting any error but the replacement is not working. Please advice what i am doing wrong Code: Copy mynewfile=File.ReadString(File.DirDefaultExternal&"/testr/","Mai.txt") mynewfile = mynewfile.Replace ("kliev", "jared") File.WriteString(File.DirDefaultExternal&"/testr/","Mai.txt",mynewfile)
klaus Expert Licensed User Longtime User Feb 27, 2015 #2 What is not working ? Put a breakpoint in the second line. Check the content of mynewfile. Is the reading OK ? Go one step further and check mynewfile again. Did the content change ? Go one step further to save the file. Add a new line reading the file back and check. Upvote 0
What is not working ? Put a breakpoint in the second line. Check the content of mynewfile. Is the reading OK ? Go one step further and check mynewfile again. Did the content change ? Go one step further to save the file. Add a new line reading the file back and check.
A ajk Active Member Licensed User Longtime User Mar 29, 2015 #3 I have the same problem Dim abc As String abc= "aaa DO bbb" abc.Replace("DO", "AAA") Et3.Text=abc and we habe Et3= "aaa DO bbb" It seems to be .replace problem. Upvote 0
I have the same problem Dim abc As String abc= "aaa DO bbb" abc.Replace("DO", "AAA") Et3.Text=abc and we habe Et3= "aaa DO bbb" It seems to be .replace problem.
klaus Expert Licensed User Longtime User Mar 29, 2015 #4 This is wrong: abc.Replace("DO", "AAA") but this works: abc = abc.Replace("DO", "AAA") Upvote 0
Erel B4X founder Staff member Licensed User Longtime User Mar 30, 2015 #5 This is a common mistake. Remember that strings are immutable (unlike StringBuilder). This means that the only way to change the string value referenced by abc is by assigning a new string to abc. Upvote 0
This is a common mistake. Remember that strings are immutable (unlike StringBuilder). This means that the only way to change the string value referenced by abc is by assigning a new string to abc.
T Troberg Well-Known Member Licensed User Longtime User Mar 30, 2015 #6 Just to make it even clearer: This code is correct: abc = abc.Replace("DO", "AAA") It does the replace and returns it, and we place it in a variable (which happens to be the same variable we started with). This code is not correct: abc.Replace("DO", "AAA") It does the replace and returns it, but we do nothing with the return value, we just throw it away. A very easy mistake to make, even if one knows it's wrong. The brain is a bit lazy sometimes, and tends to forget such details. I've done it myself, several times. Perhaps an issue the compiler should raise a warning about? I can't see any reason under any circumstance to do it the wrong way, so a warning feels apropriate. Upvote 0
Just to make it even clearer: This code is correct: abc = abc.Replace("DO", "AAA") It does the replace and returns it, and we place it in a variable (which happens to be the same variable we started with). This code is not correct: abc.Replace("DO", "AAA") It does the replace and returns it, but we do nothing with the return value, we just throw it away. A very easy mistake to make, even if one knows it's wrong. The brain is a bit lazy sometimes, and tends to forget such details. I've done it myself, several times. Perhaps an issue the compiler should raise a warning about? I can't see any reason under any circumstance to do it the wrong way, so a warning feels apropriate.