Thursday, October 6, 2022

#1 2022-04-18 04:48:32 am

davidhmorgan
Member
From:: Sydney
Registered: 2014-08-20
Posts: 92

"dogflower" contains "dog". Right?

In plain AppleScript if I type:

Applescript:


"dogflower" contains "dog"

and run it, the result is true.

So how come if I type the following:

Applescript:


set string1 to "dogflower"
set string2 to "dog"
string1 contains string2

and run it, the result is false?


Filed under: substring

Offline

 

#2 2022-04-18 05:25:34 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5570

Re: "dogflower" contains "dog". Right?

davidhmorgan wrote:

So how come if I type the following:

Applescript:


set string1 to "dogflower"
set string2 to "dog"
string1 contains string2

and run it, the result is false?


It works correctly here as posted. Result = true. Are there any typos or environmental considerations in your original script?


NG

Offline

 

#3 2022-04-18 06:07:05 am

davidhmorgan
Member
From:: Sydney
Registered: 2014-08-20
Posts: 92

Re: "dogflower" contains "dog". Right?

Well that is strange. When I pasted the text from this post into a new script it worked. Then when I went back into the first script and re typed it manually, it also worked. I'm sure there were no typos because I used the original text to paste into this post.

I pasted the text from the original script into a new script and it came back with false. But when I pasted it into TextEdit as text only and then into a new script, it came back with true.

Offline

 

#4 2022-05-27 10:47:50 am

davidhmorgan
Member
From:: Sydney
Registered: 2014-08-20
Posts: 92

Re: "dogflower" contains "dog". Right?

Update on this, I found the problem.

It happened when I captured a string by selecting a phone number in the Contacts app and using this to define a string variable in AppleScript. This happens the same whether I am editing the contact or not. Along with the copied phone number came a null character at the beginning and end of the string.

I isolated the problem down to where a test AppleScript was literally telling me that two variables which visually contained the same string value were not actually the same. I found out the reason by counting the characters in the string. One had more characters. After splitting the strings into lists of characters, "" was a member of the list.

It's the strangest thing, I didn't even know that it was possible for a string to contain a null character.

Last edited by davidhmorgan (2022-05-27 10:52:39 am)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)