Problem with FindStr()

Find quick help here to get you started with Hollywood
papiosaur
Posts: 192
Joined: Fri Mar 31, 2023 1:34 pm

Problem with FindStr()

Post 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?
papiosaur
Posts: 192
Joined: Fri Mar 31, 2023 1:34 pm

Re: Problem with FindStr()

Post by papiosaur »

Is there a special command to remove all control caracters from a string?

I see IsCntrl() command but seems don't work...
plouf
Posts: 532
Joined: Sun Feb 04, 2018 11:51 pm
Location: Athens,Greece

Re: Problem with FindStr()

Post 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
Christos
papiosaur
Posts: 192
Joined: Fri Mar 31, 2023 1:34 pm

Re: Problem with FindStr()

Post 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
plouf
Posts: 532
Joined: Sun Feb 04, 2018 11:51 pm
Location: Athens,Greece

Re: Problem with FindStr()

Post 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
Christos
papiosaur
Posts: 192
Joined: Fri Mar 31, 2023 1:34 pm

Re: Problem with FindStr()

Post 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
plouf
Posts: 532
Joined: Sun Feb 04, 2018 11:51 pm
Location: Athens,Greece

Re: Problem with FindStr()

Post 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")
Christos
User avatar
jPV
Posts: 626
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: Problem with FindStr()

Post 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.
papiosaur
Posts: 192
Joined: Fri Mar 31, 2023 1:34 pm

Re: Problem with FindStr()

Post 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")
User avatar
jPV
Posts: 626
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: Problem with FindStr()

Post 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?
Post Reply