sbonkosk
sbonkosk

Reputation: 102

Applescript: check if a string contains empty string?

I have an applescript that gets a string at one point and checks if it contains another string that I inputted.

So lets says I have "Im a string" and I want to search if it contains "string". I have this working by using "if searchString is in mainString then...."

But if I pass it an empty string this should equate to true no matter what the string is, but for some reason it isnt working.

How could I check if an empty string is in another string?

Upvotes: 0

Views: 8761

Answers (2)

Philip Regan
Philip Regan

Reputation: 5055

Different languages are going to handle this differently, and what you are seeing here is how Applescript handles it [1]. Any empty string is exactly that: empty. "" has no value in Applescript (but it's neither null or missing a value; there is just nothing in it). If you were comparing two empty strings, then they would equate to each other, otherwise either a string is empty or it isn't and populated string can't contain an empty string.

You'll have to check to see if the search string is empty first and then handle that accordingly.

[1] REALbasic won't find an empty string in a populated string either.

update per comment:

Yes, but I have alwasys found it better to do the calculations of the condition outside of the if...then block with Applescript:

set condition1 to true -- "true" being a calculation of some kind
set condition2 to true

if (condition1) and (condition2) then
    -- do something
end if

Upvotes: 2

Hans Kesting
Hans Kesting

Reputation: 39348

What about first checking whether the string-to-search-for is empty, and then return 'true'?

Upvotes: 1

Related Questions