1

I have a two word string in another view controller containing a user defined first and last name

NSString *userName = ([self hasAttributeWithName:kContractorName] ? [self attributeWithName:kContractorName].value : [self.certificate.contractor.name uppercaseString]);

when retrieving this string in another view controller I want to extract only the first name.

I researched SO on using scanner and found a very helpful answer here: Objective C: How to extract part of a String (e.g. start with '#'), and im almost there.

The problem is I can only seem extract the second name with my variation on the origial code. Im scanning my string up to the space between first and second name, this returns the second name fine. Just need a nudge now on how to set this to extract the first name instead of the second

    NSMutableArray *substrings = [NSMutableArray new];
    NSScanner *scanner = [NSScanner scannerWithString:userName];
    [scanner scanUpToString:@" " intoString:nil]; // Scan all characters before 
    while(![scanner isAtEnd]) {
        NSString *name = nil;
        [scanner scanString:@" " intoString:nil]; // Scan the  character
        if([scanner scanUpToString:@" " intoString:&name]) {
            // If the space immediately followed the , this will be skipped
            [substrings addObject:name];
        }
        [scanner scanUpToString:@" " intoString:nil]; // Scan all characters before next 
    }
Community
  • 1
  • 1
JSA986
  • 5,870
  • 9
  • 45
  • 91

4 Answers4

5

Better use NSString's componentsSeparatedByString method:

NSString* firstName = [userName componentsSeparatedByString:@" "][0];
Alexey Kozhevnikov
  • 4,249
  • 1
  • 21
  • 29
3

If first and last name are separated with a space you can use:

NSArray *terms = [userName componentsSeparatedByString:@" "];

NSString *firstName = [terms objectAtIndex:0];
JSA986
  • 5,870
  • 9
  • 45
  • 91
RFG
  • 2,880
  • 3
  • 28
  • 44
1

You could just split the string into first and last names using componentsSeparatedByString.

NSArray *subStrings = [userName componentsSeparatedByString:@" "];
NSString *firstName = [subStrings objectAtIndex:0];
Kevin
  • 16,696
  • 7
  • 51
  • 68
1

Sure, you can just split the string by spaces and take the first element, but where’s the fun in that? Try NSLinguisticTagger to actually split this using a Cocoa API:

__block NSString *firstWord = nil;

NSString *question = @"What is the weather in San Francisco?";
NSLinguisticTaggerOptions options = NSLinguisticTaggerOmitWhitespace | NSLinguisticTaggerOmitPunctuation | NSLinguisticTaggerJoinNames;
NSLinguisticTagger *tagger = [[NSLinguisticTagger alloc] initWithTagSchemes: [NSLinguisticTagger availableTagSchemesForLanguage:@"en"] options:options];
tagger.string = question;

[tagger enumerateTagsInRange:NSMakeRange(0, [question length]) scheme:NSLinguisticTagSchemeNameTypeOrLexicalClass options:options usingBlock:^(NSString *tag, NSRange tokenRange, NSRange sentenceRange, BOOL *stop) {
    firstWord = [question substringWithRange:tokenRange];
    *stop = YES;
}];
Jeff Kelley
  • 19,021
  • 6
  • 70
  • 80