Processing words individually in an NSString

Discussion in 'iOS Programming' started by gwelmarten, Apr 8, 2012.

  1. gwelmarten macrumors 6502

    Joined:
    Jan 17, 2011
    Location:
    England!
    #1
    Hi
    I am trying to run a "Find and Replace" for individual words in an NSString. I want to basically say:
    Code:
    IF words longer than 4 characters, do this method for this word.
    ELSE do this method for this word.
    where I define a word as a block of text with a space on either side of it.
    I'm getting my String from a textField with:
    Code:
    input = textField.text;
    The two bits I am not sure about are:
    1) How to split the string into individual words in such a way that I can recombine it later to make one string. Currently, I am splitting it with:
    Code:
    words = [input componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    But I am not sure how to processs the text from there.
    2) How to run an IF/ELSE that is based on the word length in an NSArray. I can do it for NSString, but getting individual components from an Array seems more difficult.
    Can anybody point me in the direction of some documentation on how to do this, or give me a few pointers? I've done some research, but can't find anything to do with individually processing an NSArray.

    Thanks in advance,
    Sam
     
  2. robvas macrumors 68020

    Joined:
    Mar 29, 2009
    Location:
    USA
    #2
    After you split the string into an array you can simply iterate over it. You'll have to make some decisions about how you want to handle things like spaces and punctuation.

    Try something like this:

    Code:
    #import <Foundation/Foundation.h>
    
    int main(int argc, const char * argv[])
    {
    
        @autoreleasepool {
            NSString *phrase = @"Can anybody point me in the direction of some documentation on how to do this, or give me a few pointers?";
            
            NSMutableString *bigWords = [[NSMutableString alloc] init];
            
            NSArray *words = [phrase componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
            
            // interate over the array of words
            for (NSString *currentWord in words) {
                if ([currentWord length] > 4) {
                    NSLog(@"The word '%@' contains more than 4 characters", currentWord);
                    bigWords = [bigWords stringByAppendingString:[NSString stringWithFormat:@"%@ ", currentWord]];
                }
                else {
                    NSLog(@"The word '%@' contains 4 or less characters", currentWord);                
                }
            }
            
            NSLog(@"The words with more than 4 characters were '%@'", bigWords);
        }
        return 0;
    }
     
  3. gwelmarten thread starter macrumors 6502

    Joined:
    Jan 17, 2011
    Location:
    England!
    #3
    Hi
    Thanks you so much! I really appreciate the time that must have gone into writing that for me.
    I'll get back with progress and show you how I've used it :)

    Sam
     
  4. gwelmarten thread starter macrumors 6502

    Joined:
    Jan 17, 2011
    Location:
    England!
    #4
    Hi
    So, I now have this code for processing words individually from an NSString. However, I can't recompile at the end :(

    Code:
    - (void)convert {
        NSString *phrase = @"Can anybody point me in the direction of some documentation on how to do this, or give me a few pointers?";
        
        NSMutableString *bigWords = [[NSMutableString alloc] init];
        
        NSArray *words = [phrase componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        
        // interate over the array of words
        for (NSString *currentWord in words) {
           if ([currentWord length] > 4) {
                currentWord =[currentWord stringByReplacingOccurrencesOfString:@"a" withString:@""];
                currentWord =[currentWord stringByReplacingOccurrencesOfString:@"e" withString:@""];
                currentWord =[currentWord stringByReplacingOccurrencesOfString:@"i" withString:@""];
                currentWord =[currentWord stringByReplacingOccurrencesOfString:@"o" withString:@""];
                currentWord =[currentWord stringByReplacingOccurrencesOfString:@"u" withString:@""];
                NSLog(@"The word '%@' contains more than 4 characters", currentWord);currentWord);
                bigWords = [bigWords stringByAppendingString:[NSString stringWithFormat:@"%@ ", currentWord]];
            }
            else {
                NSLog(@"The word '%@' contains 4 or less characters", currentWord);                
            }
        }
        
        NSLog(@"The words with more than 4 characters were '%@'", bigWords);
        output = words;
    }
    What I then want to do is output my processed text into another NSString (called output). The method I have used (see above) doesn't work - is that because the processing I have done above removes any ordering of the words? I'm not sure, but I don't think it.

    Does anybody have any ideas? I also tried compiling the two lists together into an NSString, but as expected that put all the short words followed by all the long words.
     
  5. robvas macrumors 68020

    Joined:
    Mar 29, 2009
    Location:
    USA
    #5
    You need to fix this line:
    Code:
                NSLog(@"The word '%@' contains more than 4 characters", currentWord);currentWord);
    make it look like this:
    Code:
                NSLog(@"The word '%@' contains more than 4 characters", currentWord);
    Also your 'output' variable is never initialized and doesn't do anything - are you trying to return it? You can't have a 'void' function return a variable.
     
  6. gwelmarten thread starter macrumors 6502

    Joined:
    Jan 17, 2011
    Location:
    England!
    #6
    Hi
    Thanks for the help so far - I've compiled some code for that now works, which I shall post after I have fixed this last little bit.
    I want to check if the NSString "word" is within a list of about 50 words without running IF's for all 50 :)

    In my code below, I am checking to see if "word" is in *searchForMe, where searchForMe is a list of the words that trigger that IF function.

    Code:
            NSString *searchForMe = @"great mate hate shate crate";
            NSRange range = [word rangeOfString : searchForMe];
            
            if (range.location != NSNotFound) {
                NSLog(@"I found something.");
    }
    
    However it searches only for the entire expression inside searchForMe (in other words, it only finds a match if "word" is "great mate hate shate crate". Does anybody have an idea how I could replace that NSString with a list of values, enabling me to search in the list for individual words to see if "word" is there?

    Thanks in advanced,

    Sam
     
  7. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #7
    In robvas's first example, he used a for loop. Why not use one here?

    (The fact you're even asking the question makes me wonder how familiar you are with C... it might do you good to read through / do the exercises on this website:

    http://c.learncodethehardway.org/book/

    I haven't gone through the whole book yet, but I believe that for loops are covered in exercise 10.)
     
  8. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #8
    In your "list of individual words", the "list" is an NSArray or NSMutableArray, and each "individual word" is an NSString object stored in the array. Study the NSArray class methods, and look at tutorials or chapters in books that cover the NSArray class.

    If you don't know the foundation classes, like NSArray to hold a series of items, you should study those foundation classes first. You won't be able to solve significant problems unless you know the fundamentals.

    If you're already studying a book or tutorial, please post its exact title and author, or its URL. If we don't know what you've already learned (or should have learned), it's difficult to offer specific suggestions on where to look next.
     

Share This Page