1

I am applying a search in a large content.Using NSRange to achieve this.

range = [textToSearch rangeOfString:searchText options:NSCaseInsensitiveSearch];

and then I am checking the presence of String like this:

if(range.location != NSNotFound){
   //String found
 }

Now I have only the Range for specified string. I want to display my search result as:

Suppose My Content is this:

I am a movie fanatic. When friends want to know what picture won the Oscar in 1980 or who played the police chief in Jaws, they ask me. My friends, though, have stopped asking me if I want to go out to the movies.

Now I have made a search for "Stopped"

Now I want to display my search as:

Result:

My friends, though, have stopped asking me if I want to

I want some word before and after searched result. I don't know how to acheive this.

Wain
  • 118,658
  • 15
  • 128
  • 151
Rahul
  • 5,594
  • 7
  • 38
  • 92
  • Adjust the location by subtracting the number of characters you'd like to include. Make sure to do a MAX(0, newLocation) to avoid going negative. Do the same for length, taking care not to go past the end of the string. – Avi Jan 19 '16 at 11:30
  • How to achieve this ? – Rahul Jan 19 '16 at 11:30
  • And next time, show what you've tried. SO isn't a code-writing service. – Avi Jan 19 '16 at 11:31
  • @Avi I have written the code what I have tried... and I am not able to proceed that's why I asked..If you feel I am wanting for a service then keep out of it – Rahul Jan 19 '16 at 11:34
  • So show the code you wrote and explain why it doesn't work, if you can. The answer is 4 lines of code, two of which are simple math expressions. – Avi Jan 19 '16 at 11:37
  • @Avi I found my answer my self. No need of your service – Rahul Jan 19 '16 at 11:37

1 Answers1

0

You can use a regex such as: (\w+[\s,]+){0,4}stopped([\s,]+\w+){0,6} which roughly translates to match 4 words before stopped and 6 words after.

FranMowinckel
  • 4,233
  • 1
  • 30
  • 26