Finding words within an Array

Related searches

How to check if any of words of a string exist in an array?

For example:

let arr = ["This", "is", "Array", "of", "String"]
let str = "The word String is exists"

The result should be:

Found words: "String", "is"

Here is a less naive solution that will work for strings that contain punctuation.

This first splits the string into words using localized rules. It then finds the intersection of the two arrays of words.

func findWords(text: String, words: [String]) -> [String] {
    var textWords: Set<String> = []
    text.enumerateSubstrings(in: text.startIndex..<text.endIndex, options: [.byWords, .localized]) { (word, _, _, _) in
        if let word = word {

    let matches = textWords.intersection(words)

    return Array(matches)

print(findWords(text: "The word String is exists", words: ["This", "is", "Array", "of", "String"]))
print(findWords(text: "Hello there. How are you today?", words: ["there", "is", "today"]))


["String", "is"] ["there", "today"]

As of this writing, none of the other solutions will work for the second example.

Search a Word in a 2D Grid of characters, We use two arrays x[] and y[] to find next move in all 8 directions. Below are implementation of the same: C++; Java; Python3; C#. C++. You will see that it returns an array. The array is highlighted by the squiggly brackets { } ‘s ={4,2,1,4,2,4,2,1,4,2} This is the heart of the solution. What this is showing us is that for each position in the range B2:K2, the count of how many times that cells value occurs in the range B2:K2

You can try

let str = "The word String is exists"
let all = str.components(separatedBy:" ")
let arr = ["This", "is", "Array", "of", "String"]
let res =  arr.filter { all.contains($0) } // or contains(where

Most frequent word in an array of strings, Given an array of words find the most occurring word in it. Examples: Input : arr[] = {"geeks", "for", "geeks", "a", "portal", "to", "learn", "can", "be",� The array produced by the COUNTIF is {1,1,0,1,1,0,0,0,0}, so this is finding all Networks with their position in Net_List. MATCH only finds the first match to "1" in the array that corresponds to "Wifi", hence its rank in the complete formula. I know of using SMALL to get first, second, third etc. instances but this only works line by line.

Try following:

var resArray = [String]()
let wordArr = str.components(separatedBy: " ")
for s in wordArr {
     if arr.contains(s) {

How do I find all the words with a certain letter, when I have a cell , with a certain Learn more about cell arrays, strings, character. How do I find all the words with a certain letter, when I have a cell array of strings? Follow. For each of these arrays, the Distinct method removes all duplicate words, and then the query performs an Intersect operation on the word array and the wordsToMatch array. If the count of the intersection is the same as the count of the wordsToMatch array, all words were found in the words and the original sentence is returned.

let arr = ["This", "is", "Array", "of", "String"]
let str = "The word String is exists"
let set1 = Set(str.components(separatedBy: .whitespaces).filter { !$0.isEmpty })
let set2 = Set(arr)
let result = set1.intersection(set2)

Find Text With INDEX and MATCH, When you want to find text that's buried somewhere in a string, the The array shows 0;1;0 so it found a match for CAT, which is in the A count would return 2 as two words (dog and cat) are found within the CodeList. BTW, your true array formula is far more robust and deals with situations such as having a cell which contains TWO of the words from the list. It is also more forgiving if the ROW function is fed a number of rows that doesn’t match exactly the number of rows in the “list”.

You can obtain the result directly with a set intersection:

let arr = ["This", "is", "Array", "of", "String"]
let str = "The word String is exists"
let separators = CharacterSet.alphanumerics.inverted
let result     = Set(arr).intersection(str.components(separatedBy: separators))

How to check whether a string contains any words from an array , let string = "The rain in Spain" let stringResult = string.contains("rain") both those contains() methods can be combined together to form a new meaning: “do any of the words in this array exist in this string? Find out more. For this reason, I've modified your function to use SEARCH instead of FIND), and their involvement in the project captured in another array of 12 columns. Purpose of the spreadsheet is for any user to enter a search name (e.g. Peter) and for the spreadsheet to display all projects this person is involved in.

If pattern is a character vector or string scalar with no characters ('' or ""), then strfind returns an empty array. Starting in R2016b, the contains function is recommended for finding patterns within string arrays.

It’s gotta be in here somewhere … Update: Based on questions I’ve received, I added the Misc Notes section to the end of this post. Continuing with our #FunctionFriday series, today we’re going to explore how to use the IF, SEARCH, and ISNUMBER functions together to find text (aka a string) inside other text for classification purpose.

Search in an array of strings where non-empty strings are sorted; Search strings with the help of given pattern in an Array of strings; C program to find and replace a word in a File by another given word; Longest Common Prefix using Word by Word Matching; C program to Replace a word in a text by another given word; Construct an Array of

  • What have you tried so far?
  • I would just promote arr to a Set if it gets big enough, to avoid quadratic time complexity.
  • Keep in mind that this won't work if the string contains any punctuation other than spaces.
  • @Alexander yes but since it'a already suggested no need to repeat
  • @rmaddy we may work on the current values , op should be careful in such cases
  • 1) you can simplify this code into just a simple filter statement, 2) you should use a Set for resArray (what does "res array" even mean)? 3) Don't reward questions that don't show any effort. For all you know, you just did this guy's homework without having him learn anything at all
  • Keep in mind that this won't work if the string contains any punctuation other than spaces.
  • Keep in mind that this won't work if the string contains any punctuation other than spaces.
  • You now support other whitespace besides a simple space, but it doesn't handle any other punctuation.