Page 1 of 1

CRLF removal

Posted: Fri Feb 05, 2021 2:27 am
by danwinslow
I have a string in the structure Am called Text. I want to remove any occurrences of the $0d$0a character pattern (cr/lf).

This does not seem to work. The string text is unchanged. What am I doing wrong?

Code: Select all

Am\Text=ReplaceString(Am\text, Str($0d)+Str($0a), "")
*edit*
Ach, never mind. I need Chr() I think.

*re-edit*
Er nope. Is this a UTF vs. Ascii type issue? I notice the strings are two byte characters.

*re-re-edit
Yeah, Am\Text=ReplaceString(Am\text, Str($0d)+chr(0)+Str($0a)+str(0), "") sort of works, except it replaces them with spaces (Chr(20)+Chr(0)) rather than trimming.

Re: CRLF removal

Posted: Fri Feb 05, 2021 3:31 am
by Mijikai
Use the constant:

Code: Select all

EnableExplicit

Global str.s

str = "hello world!" + #CRLF$ + "123"

MessageRequester("",str)

str = ReplaceString(str,#CRLF$,#Null$)

MessageRequester("",str)

End

Re: CRLF removal

Posted: Fri Feb 05, 2021 6:26 am
by TI-994A
danwinslow wrote:...to remove any occurrences of the $0d$0a character pattern (cr/lf)...
The PureBasic #CRLF$ string constant is just a convenient compound constant representing the carriage return character (CR = ASCII-13 = HEX-0D), and the line feed character (LF = ASCII-10 = HEX-0A). Their values can be called with the Chr() function, as follows:

Code: Select all

;line feeds
Chr(10)   ;ASCII
Chr($0A)  ;HEX

;carriage returns
Chr(13)   ;ASCII
Chr($0D)  ;HEX

;interchangeable values of compound constant #CRLF$
If #CRLF$ = Chr(13) + Chr(10) : Debug "same value" : EndIf
If #CRLF$ = Chr($0D) + Chr($0A) : Debug "same value" : EndIf
If #CRLF$ = Chr(13) + Chr($0A) : Debug "same value" : EndIf
If #CRLF$ = Chr($0D) + Chr(10) : Debug "same value" : EndIf
As per your intended search/replace usage, they all work quite interchangeably:

Code: Select all

Structure AM_struct
  text1.s
  text2.s
EndStructure

Define AM.AM_struct

AM\text1 = "A" + Chr($0D) + Chr($0A) + 
           "string" + Chr($0D) + Chr($0A) + 
           "with" + Chr($0D) + Chr($0A) + 
           "many" + Chr($0D) + Chr($0A) + 
           "linefeeds."

AM\text2 = "Another" + #CRLF$ + 
           "string" + #CRLF$ + 
           "with" + #CRLF$ + 
           "many" + #CRLF$ + 
           "linefeeds."

;finding Chr($0D)+Chr($0A) with #CRLF$
AM\text1 = ReplaceString(AM\text1, #CRLF$, " ")

;finding #CRLF$ with Chr($0D)+Chr($0A)
AM\text2 = ReplaceString(AM\text2, Chr($0D) + Chr($0A), " ")

Debug AM\text1
Debug AM\text2

Re: CRLF removal

Posted: Fri Feb 05, 2021 7:08 am
by Marc56us
I want to remove any occurrences of the $0d$0a character pattern (cr/lf).
"Think simple" :arrow: RemoveString() :mrgreen:

Code: Select all

A$ = "Hello " + #CRLF$ + "World!"
Debug A$

A$ = RemoveString(A$, #CRLF$)

Debug ""
Debug A$

Code: Select all

Hello 
World!

Hello World!
:wink:

Re: CRLF removal

Posted: Fri Feb 05, 2021 1:45 pm
by danwinslow
Thanks all.

As it mentions above, I did try replacestring, which didn't work:

Code: Select all

Am\Text=ReplaceString(Am\text, Str($0d)+Str($0a), "")
I don't know whether it was the empty string as 3rd param or not using the #CRLF$ constant, but it did not work.

Re: CRLF removal

Posted: Fri Feb 05, 2021 1:59 pm
by Kiffi
danwinslow wrote:

Code: Select all

Am\Text=ReplaceString(Am\text, Str($0d)+Str($0a), "")
read carefully:

Code: Select all

Am\Text=ReplaceString(Am\text, Chr($0d)+Chr($0a), "")
or:

Code: Select all

Am\Text=RemoveString(Am\text, Chr($0d)+Chr($0a))

Re: CRLF removal

Posted: Fri Feb 05, 2021 2:04 pm
by infratec
Your version will never work :!:

We showed you above how it works.

What does Str() :?:
It converts a number to a string

So you do Str(13) + Str(10) which results in "1310"
For testing:

Code: Select all

Debug Str($0d) + Str($0a)
Debug #CR$ + #LF$
Debug "----"

Re: CRLF removal

Posted: Fri Feb 05, 2021 4:29 pm
by mk-soft
You don't need the Chr function. For the control characters 1 to 31, these are available as constants.

Code: Select all

Debug Asc(#TAB$)
Debug Asc(#CR$)
Debug Asc(#LF$)
Debug Asc(#STX$)
Debug Asc(#ETX$)
; etc

Re: CRLF removal

Posted: Fri Feb 05, 2021 10:30 pm
by danwinslow
Yep, was using ReplaceString instead of RemoveString. RemoveString worked much better.
To the question above, Str() converts a number to its string representation.
Thanks for the help.


Dan