If any of B contains all of A, then count # of times A is in all of B

You can define a custom function.

Open 'Function Editor' tab in 'Field Calculator'. Create new file, name it. Paste this script into the editor.

from qgis.core import *
from qgis.gui import *

@qgsfunction('auto', 'Custom')
def func(field_a, field_b, feature, parent):
    return field_b.count(field_a)

Click 'Load' button. Go back to 'Expression' tab and write this expression: (You should use double quotes for field, not 'A' or 'B')

func( "A" , "B" )

enter image description here

Result:

enter image description here


Checking if A is in B can be done using regular expressions:

CASE WHEN regexp_match('QGIS ROCKS','\\sROCKS') > 0 
THEN 1
ELSE 0
END

but the counting will probably require a custom function.


We found a way to do this in Excel:

https://exceljet.net/formula/count-specific-words-in-a-cell

Generic formula:

=(LEN(text)-LEN(SUBSTITUTE(text,word,"")))/LEN(word)

Specific formula for the example above:

=(LEN(B1)-LEN(SUBSTITUTE(B1,A1,"")))/LEN(A1)