1

What I want to do is to check if an entire word is present in a string. Let me give you an example.

let mainString = "this is a my string"
let searchString = "str"
if mainString.containsString(searchString) {

}

Here this condition will be true but I do not want this. I want it to be true when either "this" or "is" or "a" or "my" or "string" is searched in the mainString meaning I want to compare the whole word not the characters within the string. I hope I have elaborated it.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
kashif789us
  • 474
  • 8
  • 23

3 Answers3

2

// The following method will return a string without punctuation and non-required stuff characters

Source of information : How to remove special characters from string in Swift 2?

 func removeSpecialCharsFromString(mainString) -> String {
    let okayChars : Set<Character> = 
        Set("abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLKMNOPQRSTUVWXYZ".characters)
    return String(text.characters.filter {okayChars.contains($0) })
} 



let stringWithoutSpecialChars = removeSpecialCharsFromString(mainString)

// Source of info : Chris's answer on this page

let components = stringWithoutSpecialChars.componentsSeparatedByString(" ")
for component in components {
    print(component)
}

enter image description here

Community
  • 1
  • 1
Akshansh Thakur
  • 5,163
  • 2
  • 21
  • 38
  • It won't works if the sentence ends by "str" or if there is a ponctuation sign after "str". – Maxime May 29 '16 at 07:14
  • @Maxime try this, maybe if will work. – Akshansh Thakur May 29 '16 at 07:33
  • @Maxime, I tested. This is working fine. – Akshansh Thakur May 29 '16 at 07:44
  • *Remark:* a-z are not the only letters used in the world. German has äöü, french has éàû, greek has αβγ, just to note a few :) – Martin R May 29 '16 at 09:05
  • I agree @MartinR. But, at the same time, the Set of characters provides us a flexible way to add them easily. – Akshansh Thakur May 29 '16 at 09:11
  • I would recommend using NSCharacterSets. Martin's comment regarding foreign characters can be more easily handled by creating an inverse set from the characters you want to exclude, for example, Filtering `NSCharacterSet.punctuationCharacterSet().invertedSet` may work effectively to remove punctuation. I won't update my answer to include anything regarding filtering of works, as it's not indicated as significant within the scope of the original question. – Chris Conway May 29 '16 at 10:29
1

You have to use a Regex like that :

NSString *pattern = @"\\str\\b";
NSRange range = [text rangeOfString:pattern options:NSRegularExpressionSearch|NSCaseInsensitiveSearch];

The \b at the end of the pattern do what you want, it will match only whole words.

Maxime
  • 1,332
  • 1
  • 15
  • 43
1

I would recommend splitting your string into components and seeing if a component matches your string. Below is a method that uses components to check if any word matches a given search term.

func isTermInString(term:String, stringToSearch:String) -> Bool {
    let components = stringToSearch.componentsSeparatedByString(" ")

    for component in components {
        if component == term {
            return true
        }
    }

    return false
}
Chris Conway
  • 1,028
  • 7
  • 18
  • what if the sentence is "this is str!". then you would get "this", "is" "str!". Wont work in that case – Akshansh Thakur May 29 '16 at 07:19
  • Correct, `str` isn't the same word as `str!`, as `!` is a character, much like any other letter. If that was a significant requirement for the search, you could remove non-alphanumeric characters using a method similar to the question in this link: http://stackoverflow.com/questions/713918/replace-multiple-characters-in-a-string-in-objective-c – Chris Conway May 29 '16 at 07:22