Removing Nulls from strings

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Simon Knight
Posts: 854
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Removing Nulls from strings

Post by Simon Knight » Mon Apr 11, 2022 7:58 am

I have just been bitten by the null character which has caused me to lose the extension from four thousand odd image files. Thankfully I have a back up but owing to complications the restore process is going to take approximately twenty four hours. So this is important to me.

I thought I had trapped the null character but it appears to me that the command Replacetext(tString,null,"") does not work.

I believe that this is a bug or at least a serious limitation of the command but seek confirmation just in case I have made a silly error.

I have attached a stack that contains part of a file name that has Ascii 0 after a "W" character in a field. The stack uses two commands that should remove the null but only the second does.

What do you think?

S
Null-Hunt.livecode.zip
Zip of a Livecode stack
(1.58 KiB) Downloaded 72 times
best wishes
Skids

stam
Posts: 2679
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Removing Nulls from strings

Post by stam » Mon Apr 11, 2022 9:55 am

Simon Knight wrote:
Mon Apr 11, 2022 7:58 am
I thought I had trapped the null character but it appears to me that the command Replacetext(tString,null,"") does not work.
<snip>
I believe that this is a bug or at least a serious limitation of the command but seek confirmation just in case I have made a silly error.
<snip>
As I recently realised in my bug triage post here, replaceText uses regex!
This is actually expected behaviour and not a bug ;)
DICTIONARY entry for replaceText wrote:Summary: Searches for a regular expression and replaces the portions that match the regular expression.
As \0 is the regex form for the null character, the correct form is:

Code: Select all

put replacetext(tProblem, "\0","") into tProblemCopy
A quick test with your stack shows that this works but do confirm...
HTH
Stam

Simon Knight
Posts: 854
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Re: Removing Nulls from strings

Post by Simon Knight » Mon Apr 11, 2022 10:51 am

as \0 is the regex form for the null character, the correct form is:
Good to know but it would be ever better if the compiler recognised "null" as such when used with the command.

S
best wishes
Skids

stam
Posts: 2679
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Removing Nulls from strings

Post by stam » Mon Apr 11, 2022 11:10 am

Simon Knight wrote:
Mon Apr 11, 2022 10:51 am
Good to know but it would be ever better if the compiler recognised "null" as such when used with the command.
That's a slightly unfair comment ;)
The documentation specifies this is for regex, not free text search. The fact that it very often works with normal strings is only because unwittingly this often coincides with a string that can be used with regular expressions.

Even the autocomplete suggestion tells you this:
replaceText(stringToChange, matchExpression, replacementString)
It might be a bit much to ask the engine to do regex but then also replace keywords for regular expressions on your behalf. Null is simple, but where does that end? And how would the engine differentiate null from meaning the string \0 or the function numToNativeChar(0)?

As you already point out in your stack there are non-regex ways of doing this that do work, and keywords like null are recognised automatically, so you don't have to rely on replaceText()...

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9359
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Removing Nulls from strings

Post by richmond62 » Mon Apr 11, 2022 11:12 am

Screen Shot 2022-04-11 at 1.11.13 PM.png
-

Code: Select all

on mouseUp
   put empty into fld "FIXED"
   put fld "ProblemName" into PN
   repeat until PN is empty
      put char 1 of PN into FX
      delete char 1 of PN
      put codePointToNum(FX) into MNUM
      if MNUM is (0x0) then
         --- do nix ---
      else
         put FX after fld "FIXED"
         end if
   end repeat
end mouseUp
Last edited by richmond62 on Mon Apr 11, 2022 11:23 am, edited 1 time in total.

stam
Posts: 2679
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Removing Nulls from strings

Post by stam » Mon Apr 11, 2022 11:15 am

No need for all that Richmond, Simon already has a working version of this that is simply one line:

Code: Select all

replace null with "" in tProblem
his issue was that replaceText(tProblem, null, "") doesn't work. That's because the find criterion is regex and not free text or keywords, so the syntactically correct way of doing this is

Code: Select all

replaceText(tProblem, "\0", "")
Last edited by stam on Mon Apr 11, 2022 11:33 am, edited 1 time in total.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9359
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Removing Nulls from strings

Post by richmond62 » Mon Apr 11, 2022 11:23 am

No need for all that Richmond
Possibly: BUT my method replaces the NULL character with NOTHING while that
other method replaces it with a SPACE.
-
Screen Shot 2022-04-11 at 1.20.32 PM.png
-
Attachments
Null By Mouth.livecode.zip
Stack.
(1.4 KiB) Downloaded 72 times

stam
Posts: 2679
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Removing Nulls from strings

Post by stam » Mon Apr 11, 2022 11:27 am

richmond62 wrote:
Mon Apr 11, 2022 11:23 am
Possibly: BUT my method replaces the NULL character with NOTHING while that
other method replaces it with a SPACE.
the replacement string is "", not " ". Nothing is being replaced with a space!
You can just change this to empty if that makes it clearer :)

Code: Select all

replace null with empty in tProblem
or

Code: Select all

replaceText(tProblem, "\0", empty)

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9359
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Removing Nulls from strings

Post by richmond62 » Mon Apr 11, 2022 11:32 am

the replacement string is "", not " ".
Aha.

Don't you get a nice, warm, comfy feeling that that problem can be solved very easily in at least 2 ways? 8)

Simon Knight
Posts: 854
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Re: Removing Nulls from strings

Post by Simon Knight » Mon Apr 11, 2022 12:26 pm

# Stam - its a fair cop, but I'm still mad with myself for creating a way of trapping the error that did not work - grrrrrrr

#Richmond62 Love the button by the way!

S
best wishes
Skids

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Removing Nulls from strings

Post by dunbarx » Mon Apr 11, 2022 3:23 pm

Stam.

I read your posts here with interest. I see that "replaceText" lives strictly within regex. But, since "null' is a constant in LC, then could not the OP simply use the "replace" command?

Code: Select all

replace null with "" in yourText
Craig

Klaus
Posts: 13824
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Removing Nulls from strings

Post by Klaus » Mon Apr 11, 2022 3:26 pm

Yep, that's what Stam proposed and wrote a couple of hours ago. :-)

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Removing Nulls from strings

Post by dunbarx » Mon Apr 11, 2022 3:28 pm

Ah.

OK. Missed that.

Craig

stam
Posts: 2679
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Removing Nulls from strings

Post by stam » Mon Apr 11, 2022 3:39 pm

Yep both ways are valid and largely up to personal taste but if you're a bit handy with regex then replaceText() comes into it's own.

On the matter of regex, http://regex101.com is my currently favourite tool, not in the least as it has a live explanation of each symbol as you type and a handy searchable quick reference - for example just type null and you'll get the info you'll need and so on. But regex still bends my mind a bit ;)

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9359
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Removing Nulls from strings

Post by richmond62 » Mon Apr 11, 2022 7:41 pm

both ways are valid
All 3 ways are valid!

Post Reply

Return to “Talking LiveCode”