Removing Nulls from strings
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
- Posts: 854
- Joined: Wed Nov 04, 2009 11:41 am
- Location: Gunthorpe, North Lincs, UK
Removing Nulls from strings
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
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
best wishes
Skids
Skids
Re: Removing Nulls from strings
As I recently realised in my bug triage post here, replaceText uses regex!Simon Knight wrote: ↑Mon Apr 11, 2022 7:58 amI 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>
This is actually expected behaviour and not a bug
As \0 is the regex form for the null character, the correct form is:DICTIONARY entry for replaceText wrote:Summary: Searches for a regular expression and replaces the portions that match the regular expression.
Code: Select all
put replacetext(tProblem, "\0","") into tProblemCopy
HTH
Stam
-
- Posts: 854
- Joined: Wed Nov 04, 2009 11:41 am
- Location: Gunthorpe, North Lincs, UK
Re: Removing Nulls from strings
Good to know but it would be ever better if the compiler recognised "null" as such when used with the command.as \0 is the regex form for the null character, the correct form is:
S
best wishes
Skids
Skids
Re: Removing Nulls from strings
That's a slightly unfair commentSimon Knight wrote: ↑Mon Apr 11, 2022 10:51 amGood to know but it would be ever better if the compiler recognised "null" as such when used with the command.
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:
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)?replaceText(stringToChange, matchExpression, replacementString)
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()...
-
- Livecode Opensource Backer
- Posts: 9359
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: Removing Nulls from strings
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.
Re: Removing Nulls from strings
No need for all that Richmond, Simon already has a working version of this that is simply one line:
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
replace null with "" in tProblem
Code: Select all
replaceText(tProblem, "\0", "")
Last edited by stam on Mon Apr 11, 2022 11:33 am, edited 1 time in total.
-
- Livecode Opensource Backer
- Posts: 9359
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: Removing Nulls from strings
Possibly: BUT my method replaces the NULL character with NOTHING while thatNo need for all that Richmond
other method replaces it with a SPACE.
- -
- Attachments
-
- Null By Mouth.livecode.zip
- Stack.
- (1.4 KiB) Downloaded 72 times
Re: Removing Nulls from strings
the replacement string is "", not " ". Nothing is being replaced with a space!richmond62 wrote: ↑Mon Apr 11, 2022 11:23 amPossibly: BUT my method replaces the NULL character with NOTHING while that
other method replaces it with a SPACE.
You can just change this to empty if that makes it clearer
Code: Select all
replace null with empty in tProblem
Code: Select all
replaceText(tProblem, "\0", empty)
-
- Livecode Opensource Backer
- Posts: 9359
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: Removing Nulls from strings
Aha.the replacement string is "", not " ".
Don't you get a nice, warm, comfy feeling that that problem can be solved very easily in at least 2 ways?
-
- Posts: 854
- Joined: Wed Nov 04, 2009 11:41 am
- Location: Gunthorpe, North Lincs, UK
Re: Removing Nulls from strings
# 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
#Richmond62 Love the button by the way!
S
best wishes
Skids
Skids
-
- VIP Livecode Opensource Backer
- Posts: 9648
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Removing Nulls from strings
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?
Craig
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
Re: Removing Nulls from strings
Yep, that's what Stam proposed and wrote a couple of hours ago.
-
- VIP Livecode Opensource Backer
- Posts: 9648
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Removing Nulls from strings
Ah.
OK. Missed that.
Craig
OK. Missed that.
Craig
Re: Removing Nulls from strings
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
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
-
- Livecode Opensource Backer
- Posts: 9359
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: Removing Nulls from strings
All 3 ways are valid!both ways are valid