Applescript:检查字符串是否包含空字符串?

发布于 2024-10-04 02:30:41 字数 230 浏览 3 评论 0原文

我有一个苹果脚本,它在某个时刻获取一个字符串,并检查它是否包含我输入的另一个字符串。

所以可以说我有“我是一个字符串”,我想搜索它是否包含“字符串”。我通过使用“if searchString is in mainString then...”来实现此目的,

但是如果我传递一个空字符串,则无论字符串是什么,它都应该等于 true,但由于某种原因它不起作用。

如何检查空字符串是否在另一个字符串中?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

二智少女猫性小仙女 2024-10-11 02:30:41

不同的语言会以不同的方式处理这个问题,您在这里看到的是 Applescript 如何处理它 [1]。任何空字符串都是这样的:空。 "" 在 Applescript 中没有值(但它既不是 null 也不是缺少值;里面什么也没有)。如果您要比较两个空字符串,那么它们将彼此相等,否则字符串要么为空,要么不是,并且填充的字符串不能包含空字符串。

您必须首先检查搜索字符串是否为空,然后进行相应处理。

[1] REALbasic 也不会在填充字符串中找到空字符串。

按评论更新

是的,但我总是发现在 if...then外部进行条件计算会更好使用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

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
左耳近心 2024-10-11 02:30:41

首先检查要搜索的字符串是否为空,然后返回“true”怎么样?

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文