-4

your suggestions will be highly appreciated ..... my question is that let say i have this sentence in my UITEXTVIEW " i want to work out for the sake of good health because work is a best way to make you physically fit and work also makes you able to do your best in your job. "

now i want to delete word work in first line 4th word randomly not statically then how can i do this . is this good to use nsarray and word by word store in array after that remove but its not working .

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • I can only barely understand what you're saying, but what you want to do (to the extent that it makes sense) would be simple if you actually understood Objective-C (and programming in general). – Hot Licks Jan 14 '15 at 21:14
  • Read the docs for `NSString` and `NSMutableString`. There are plenty of ways to search strings and modify them. – rmaddy Jan 14 '15 at 21:29
  • i just want to do that i am using multiple arrays those storing same words and mapping all arrays on UITEXTVIEW if i remove a word from second array by using stringByreplacingoccurencesofstring from uitextview this has to remove also from other arrays the same word ..... this is issue – saleem abbas Jan 14 '15 at 22:05
  • Sorry but your description isn't clear. It would be best to update your question with the relevant code. Explain what data you have, what you want to do to it, and what you are getting instead. The more details the better. – rmaddy Jan 14 '15 at 22:36
  • 1
    finally i got my solution from your first answer by using nsmutablestring thank you so much . you r doing your great job STAY BLESSED . – saleem abbas Jan 14 '15 at 23:08

1 Answers1

0

Ok well here is an answer which works and although your question is slightly vague I think I got what you wanted.

So the difficulty is finding the index of the words and then choosing a random one of those. This means we can't just use find and replace as we only want to replace one.

In this answer I first find the index of the occurrence of each, then put these indexes into an array before randomly picking on out. I then use this index along with the length of the word to find and replace the word:

NSString * string=[NSString stringWithFormat:@"i want to work out for the sake of good health because work is a best way to make you physically fit and work also makes you able to do your best in your job."];

NSString * word = @"work";

NSMutableArray * indexes = [NSMutableArray new];

NSRange searchRange = NSMakeRange(0, string.length);
NSRange foundRange;

// This function loops through and finds the index of the word
while (searchRange.location < string.length) {

    searchRange.length = string.length - searchRange.location;

    foundRange = [string rangeOfString:word options:nil range:searchRange];

    if (foundRange.location != NSNotFound) {

        // Add the index to the array
        [indexes addObject:[NSNumber numberWithInteger:foundRange.location]];

        // found an occurrence of the substring! do stuff here
        searchRange.location = foundRange.location+foundRange.length;
    } else {
        // no more substring to find
        break;
    }
}

// Calculate the random index we'll use
NSUInteger randomIndex = arc4random() % [indexes count];

NSInteger stringIndex = [indexes[randomIndex] integerValue];

NSUInteger rangeIntStart = stringIndex;
NSUInteger  rangeIntFinish = word.length + 1;

NSString * result = [string stringByReplacingCharactersInRange:NSMakeRange(rangeIntStart, rangeIntFinish) withString:@""];

NSLog(@"%@",result);

Hopefully this will be a more complete answer for anyone looking in the future for this question.

I can see in the comments you might have already found an answer, if you have then either add it and answer you question or accept another answer which answers the question to allow others to benefit from what you have worked out.

sam_smith
  • 6,023
  • 3
  • 43
  • 60
  • Props to PengOne: http://stackoverflow.com/questions/7033574/find-all-locations-of-substring-in-nsstring-not-just-first for the complicated string finding code in the middle – sam_smith Jan 14 '15 at 23:43