# How to find “intersection” of two strings?

Simple question for all you experts:

String A: "I like cherries, apples, berries"
String B: "I like cherries, apples, berries, cookies"

I would like to find the text in string B but not String A(the odd one out)

I am trying to find-> ", cookies"

Is there an AppleScript that can solve this for me?

• That would be the difference, not the intersection. – CJK Mar 25 '18 at 22:25
• You need to clarify what it is you're really after! – user3439894 Mar 26 '18 at 14:39

In the absence of any other examples or formal criteria for how matching ought to be performed, I've just given an example here of the simplest way to obtain the difference between two strings:

``````    set A to "I like cherries, apples, berries"
set B to "I like cherries, apples, berries, cookies"

set the text item delimiters to {null, A}

return text items of B as text --> ", cookies"
``````

This works if one string is contained entirely within (i.e. is a substring of) the other, as was the case with your given strings. If `string A` were to be `"But I like cherries, apples, berries" or "I like cherries, apples, berries.", the above method will not work.

It also won't work if `string B` contains the word "null" where `string A` does not.

Here is another option...

``````set stringA to "I like cherries, apples, berries"
set stringB to "I like cherries, apples, berries, cookies"
set stringACharacters to count of text items of stringA

set oddManOut to text -1 thru (stringACharacters + 1) of stringB
``````

Here is another way, while using AppleScript's `do shell script` command, and `awk`:

``````set strA to "I like cherries, apples, berries"
set strB to "I like cherries, apples, berries, cookies"

do shell script "awk -F" & quoted form of strA & " '{print \$2}'<<<" & quoted form of strB
``````

When run in Script Editor, with (Events) (Replies) selected in the [Event Log], it outputs:

``````tell current application
do shell script "awk -F'I like cherries, apples, berries' '{print \$2}'<<<'I like cherries, apples, berries, cookies'"