0

I need to check if the user is typing a word correctly in a UITextView but the word is repeated multiple times. The rangeOfString() and containsString() methods for strings either check for the specified strings at their first or last occurrence, but I want to check for ALL occurrences of the string.

For example:

var string = "Hi Bloog hi Bloog hi Bloog"

I want to check if "Bloog" is present in the string, and if it is, it should be exactly "Bloog" and not "Bloogg" or "Bloo g" whenever it is repeated How do I do that? Thanks

Dieblitzen
  • 544
  • 4
  • 22
  • 2
    you want to check if interesting is spelt correctly in all the instances? How do you decide if the person was trying to spell interesting? For instance: ` "Hi dieblitzen are you interested[1] in my interests[2] to engage in interestin[3] and intelligent[4] activities?`. How do you decide which one the user was trying to spell interesting, in my example, is it 1, 2 or 3? Heck is it 4? What degree of similarity are you looking for? – pelumi Oct 11 '15 at 09:21
  • No for example if the user has to type in something fixed, like in a text challenge, and I know what he is supposed to type, but I want to make sure that "interesting" is exactly like this whenever he types it – Dieblitzen Oct 11 '15 at 09:23
  • okay, that's slightly clearer. Will that be a single word input or a string with many tokens as in your example? – pelumi Oct 11 '15 at 10:05
  • In addition, does apple developer spellcheck page help? https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/SpellCheck/Tasks/CheckingTextSpelling.html – pelumi Oct 11 '15 at 10:08
  • Not really... I don't what I really want is to see if given word is present in the string, and if it is, it should be exactly the word I want regardless of how many times it is repeated. I used spelling only as an example. I edited the question @pelumi – Dieblitzen Oct 11 '15 at 12:33
  • An option is to split the string into an array, then iterate over elements of the array. For each of the elements compare it to your target string if they are the same. pseudo code `split list using space character` `for each item in list check if it is equal to your expected word e.g. Bloog in your last example`. `If you find it there, do whatever you want to do with it.` – pelumi Oct 11 '15 at 20:35
  • Here is [how to split a string into an array](http://stackoverflow.com/questions/25678373/swift-split-a-string-into-an-array) and here is an answer on how to loop through an array: [How to loop through items in an array](https://www.hackingwithswift.com/example-code/arrays/how-to-loop-through-items-in-an-array) – pelumi Oct 11 '15 at 20:37

0 Answers0