[SOLVED] FindString() with CRLF in the search?

Just starting out? Need help? Post your questions and find answers here.
Randy Walker
Addict
Addict
Posts: 1054
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

[SOLVED] FindString() with CRLF in the search?

Post by Randy Walker »

Is there a trick to doing a search using findString() to find a string that is prefaced with CRLF? I tried something like this but no joy:

Code: Select all

search = #LFCR$+"Word"
If OpenFile(0,filename$)
  text.s = ReadString(0,#PB_Ascii | #PB_File_IgnoreEOL)
  CloseFile(0)
EndIf
Debug FindString(text,search)
For my test, the filename$ looks like this:

Code: Select all

first line
Word beginning second line.
last line.
Last edited by Randy Walker on Thu Aug 28, 2025 5:07 am, edited 1 time in total.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Cyllceaux
Enthusiast
Enthusiast
Posts: 511
Joined: Mon Jun 23, 2014 1:18 pm

Re: FindString() with CRLF in the search?

Post by Cyllceaux »

Hi,

Easy... it is #CRLF$ not #LFCR$

Code: Select all

search.s = #CRLF$+"Word" ; <--- Here
If OpenFile(0,"c:\temp\test.txt")
  text.s = ReadString(0,#PB_Ascii | #PB_File_IgnoreEOL)
  CloseFile(0)
EndIf
Debug FindString(text,search) ; 11
Last edited by Cyllceaux on Wed Aug 27, 2025 5:35 am, edited 1 time in total.
User avatar
idle
Always Here
Always Here
Posts: 5886
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: FindString() with CRLF in the search?

Post by idle »

maybe just search for #LF$+"word"
not all files will have #CRLF$
Randy Walker
Addict
Addict
Posts: 1054
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: FindString() with CRLF in the search?

Post by Randy Walker »

idle wrote: Wed Aug 27, 2025 5:35 am maybe just search for #LF$+"word"
not all files will have #CRLF$
tried that and still no joy. I inspected the file using hex editor and definitely see 0d 0a 57 6f 72 64 so the #CRLF$ should be proper.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
User avatar
idle
Always Here
Always Here
Posts: 5886
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: FindString() with CRLF in the search?

Post by idle »

What happens if you Debug the search string
Does FindString need a flag to skip #crlf$
Sorry I'm away from the computer :|
User avatar
jacdelad
Addict
Addict
Posts: 2003
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: FindString() with CRLF in the search?

Post by jacdelad »

Whenever im not sure what kind of new line is used (LF, CR, CRLF...) I replace all LF with CR and then all CRCR repeatedly with CR. Now I can search for CR+string (given that empty lines don't play a role).
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
TassyJim
Enthusiast
Enthusiast
Posts: 186
Joined: Sun Jun 16, 2013 6:27 am
Location: Tasmania (Australia)

Re: FindString() with CRLF in the search?

Post by TassyJim »

It works for me. PB6.21

Code: Select all

search.s = #CRLF$+"Word"
filename$ = "C:\\apps\teststr.txt"
If OpenFile(0,filename$)
  text.s = ReadString(0,#PB_Ascii | #PB_File_IgnoreEOL)
  CloseFile(0)
EndIf
text2.s = "first line"+#CRLF$+"Word beginning second line."+#CRLF$+"last line."

Debug StringByteLength(text)    ;102
Debug StringByteLength(text2)   ;102
Debug Len(text)                 ;51
Debug Len(text2)                ;51
Debug FindString(text,search)   ;11
Debug FindString(text2,search)  ;11
I compared a file read and a string.
Both len() and stringbytelength() included the #CRLF$ pairs in the result.
Findstring gives the correct answer including the #CRLF$

Jim
Little John
Addict
Addict
Posts: 4785
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: FindString() with CRLF in the search?

Post by Little John »

FindString() treats the characters CR and LF in the same way as most other characters, e.g. ‘a’ or ‘b’.
Randy Walker
Addict
Addict
Posts: 1054
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: FindString() with CRLF in the search?

Post by Randy Walker »

idle wrote: Wed Aug 27, 2025 7:17 am What happens if you Debug the search string
Does FindString need a flag to skip #crlf$
Sorry I'm away from the computer :|
When I "debug search" I get a line feed followed by the next line beginning with "Word".

This:

Code: Select all

search.s = #CRLF$+"Word" ; <--- Here
If OpenFile(0,"c:\Tmp\test.txt")
  text.s = ReadString(0,#PB_Ascii | #PB_File_IgnoreEOL)
  CloseFile(0)
EndIf
Debug FindString(text,search)
Debug search
Gives me this:

Code: Select all

0  ;Top line in debug window
   ;Blank line
Word ;Bottom line
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Randy Walker
Addict
Addict
Posts: 1054
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: FindString() with CRLF in the search?

Post by Randy Walker »

TassyJim wrote: Wed Aug 27, 2025 8:08 am It works for me. PB6.21
Jim
Ok, I guess (like always) I should have mentioned I was using PB6.20
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Randy Walker
Addict
Addict
Posts: 1054
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: FindString() with CRLF in the search?

Post by Randy Walker »

Little John wrote: Wed Aug 27, 2025 9:28 am FindString() treats the characters CR and LF in the same way as most other characters, e.g. ‘a’ or ‘b’.
Ok but, I still get zero when I reduce down to search.s = #CRLF$
(again, I am using PB6.20)
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
SMaag
Enthusiast
Enthusiast
Posts: 320
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: FindString() with CRLF in the search?

Post by SMaag »

k but, I still get zero when I reduce down to search.s = #CRLF$
(again, I am using PB6.20)
The problem seems to be the String you read from the file.

Code: Select all

search.s = #CRLF$ + "Word"

txt.s = "blablablablabla" + #CRLF$ + "Word"

p = FindString(txt, search)

Debug  p

Randy Walker
Addict
Addict
Posts: 1054
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: FindString() with CRLF in the search?

Post by Randy Walker »

TassyJim wrote: Wed Aug 27, 2025 8:08 am It works for me. PB6.21

Jim
I uninstalled 6.20, installed 6.21 and now seems to be working OK
Thanks Jim
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Post Reply