Page 1 of 2
Problem with FindStr()
Posted: Sat Aug 10, 2024 1:14 pm
by papiosaur
Hello,
i would like to search a substring in a string (a file converted into a string with filetostring command) with
FindStr() but i have this error message:
Invalid UTF-8 string in argument1! There are some 'ESC' special caracters in the string...
Possibilty to resolve this problem please?
Re: Problem with FindStr()
Posted: Sat Aug 10, 2024 4:08 pm
by papiosaur
Is there a special command to remove all control caracters from a string?
I see
IsCntrl() command but seems don't work...
Re: Problem with FindStr()
Posted: Sat Aug 10, 2024 6:05 pm
by plouf
yes it works here is an example to detect if there is a control character (character 10 = Line Feed)
and removing it
Code: Select all
str$ = "123"..Chr(10).."567"
DebugPrint(str$)
DebugPrint("Is Control char in position ? = "..IsCntrl(str$,3))
str$ = ReplaceStr(str$,Chr(10),"")
DebugPrint(str$)
p.s. What os ? AmiaOSes have some limitation in UTF
Re: Problem with FindStr()
Posted: Sat Aug 10, 2024 6:34 pm
by papiosaur
Thanks plouf for your answer!
Bizarely
newstr$=ReplaceStr(str$,Chr(27),"") don't work...
newstr$=patternreplaceStr(str$,Chr(27),"") work...
i'm on MorphOS
Re: Problem with FindStr()
Posted: Sat Aug 10, 2024 6:55 pm
by plouf
can you provide a non working example ?
this one works in windwos 10 and morphos 3.18
message says length = 6
Code: Select all
str$ = "123"..Chr(7).."567"
DebugPrint(str$)
DebugPrint("Is Control char in position ? = "..IsCntrl(str$,3))
str$ = ReplaceStr(str$,Chr(7),"")
SystemRequest("length "..StrStr(StrLen(str$)),"of this String > ".. str$,"OK")
Ps also in chr27
Re: Problem with FindStr()
Posted: Sat Aug 10, 2024 9:35 pm
by papiosaur
Sorry, i have no example but you can try with my file to remove Chr(27) with
ReplaceStr() to see if it work at your home:
https://www.morphos-storage.net/upload/dev/ESC.txt
Re: Problem with FindStr()
Posted: Sun Aug 11, 2024 7:04 pm
by plouf
i suspect wrong usage of
ReplaceStr(), thats why a example always help..
here is a example that tested and works in windows and morphos
NOTE that
ReplaceStr() search ALL string and replace not in current position only
Code: Select all
str$ =FileToString("f:\\ESC.txt")
len = StrLen(str$)-1
DebugPrint("before "..FindStr(str$,Chr(27)))
For i=0 To 32
str$ = ReplaceStr(str$,Chr(i),"")
Next
DebugPrint("After "..FindStr(str$,Chr(27)))
SystemRequest("original = "..StrStr(len).. " after "..StrStr(StrLen(str$)),str$,"OK")
Re: Problem with FindStr()
Posted: Sun Aug 11, 2024 9:06 pm
by jPV
Hmm.. I don't seem to have a problem with that ESC.txt and FindStr... would be nice to get a minimal example code where it fails. Now it sounds that you are fixing something that really isn't broken. And in any case there's always a way to read data without modifying it.
Re: Problem with FindStr()
Posted: Mon Aug 12, 2024 8:03 am
by papiosaur
Hello,
thank plouf for your code, i will test it on MorphOS.
This is my simple code doesn't work:
Code: Select all
text$ = filetostring("ram:toto.txt")
s$ = ReplaceStr(text$, Chr(27), "")
stringtofile(s$, "ram:toto2.txt")
Re: Problem with FindStr()
Posted: Mon Aug 12, 2024 8:21 am
by jPV
papiosaur wrote: ↑Mon Aug 12, 2024 8:03 am
This is my simple code doesn't work:
Code: Select all
text$ = filetostring("ram:toto.txt")
s$ = ReplaceStr(text$, Chr(27), "")
stringtofile(s$, "ram:toto2.txt")
For me it looks to work. When using the ESC.txt file the toto2.txt file is 10 bytes smaller than the original. And in the original there are 10 escape characters that have been removed from the result. All OK as supposed to be.
But how is this related to the original question about
FindStr() and UTF-8 error, a work-around? How to reproduce the original issue to get that solved first?