0

I have a string like @"Greetings from Capt. Ashim Mittra,​ Vice President – Flight Operations" from which I want to extract “Capt. Ashim Mittra”. i.e. I want to start from the word “from” and read to “,” (comma)

Jed Fox
  • 2,979
  • 5
  • 28
  • 38
raj
  • 39
  • 1
  • 10
  • 1
    Show code that you have tried to do so. – Bharat Modi Aug 01 '16 at 12:22
  • `substringWithRange` and `rangeOfString:` can be used. For the "bold" UI, it depends. You can use `NSAttributedString` if there are some bold text or not, or just use the `font` property of `UILabel`/`UITextView`... – Larme Aug 01 '16 at 12:22
  • How about using a regular expression? – Eiko Aug 01 '16 at 12:26
  • Possible duplicate of [How to find the substring between two string?](http://stackoverflow.com/questions/15339174/how-to-find-the-substring-between-two-string) – Andrew Aug 01 '16 at 15:38

4 Answers4

2

You can use regular expressions to find the names - here is an example:

    NSString *yourString = @"Greetings from Capt. Ashim Mittra,​ Vice President – Flight Operations";
    NSError *error = NULL;

    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"from\\s+([^,]+)"
                                                                           options:NSRegularExpressionCaseInsensitive error:&error];

    [regex enumerateMatchesInString:yourString
                            options:0
                              range:NSMakeRange(0, [yourString length])
                         usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){

                             NSString *name = [yourString substringWithRange:[match rangeAtIndex:1]];
                             NSLog(@"%@",name);

                         }];

This probably needs to be improved quite a bit, but you need to check all your input data to make the correct adjustments. It will find several names in the input string as it stands.

Eiko
  • 25,601
  • 15
  • 56
  • 71
1

Use this code :

NSString * yourStr = @"Greetings from Capt. Ashim Mittra,​ Vice President – Flight Operations";
NSRange range1 = [yourStr rangeOfString:@"from"];
NSRange range2 = [yourStr rangeOfString:@","];
NSRange rangeSubString = NSMakeRange(range1.location + range1.length, range2.location - range1.location - range1.length);
NSString *finalString = [yourStr substringWithRange: rangeSubString];

To make it bold use this;

NSMutableAttributedString * yourAttributedString = [[NSMutableAttributedString alloc] initWithString: finalString];
[yourAttributedString addAttribute: NSFontAttributeName value:[UIFont boldSystemFontOfSize:12] range:NSMakeRange(0,finalString)];
[yourLbl setAttributedText: yourAttributedString];
Pranav
  • 701
  • 4
  • 18
0

You can do something like,

   NSString *str = @"Greetings from Capt. Ashim Mittra,​ Vice President – Flight Operations";

NSRange range1 = [str rangeOfString:@"from"];
NSRange range2 = [str rangeOfString:@","];
NSRange rangeToSubString = NSMakeRange(range1.location + range1.length, range2.location - range1.location - range1.length);

NSString *resultStr = [str substringWithRange:rangeToSubString];

NSLog(@"path1 : %@",resultStr);

you can set attributed text to your label or else when you want to show your text with bold portion something like,

  UIFont *font = [UIFont boldSystemFontOfSize:17.0]; // whatever size, can use diiferent font with different method

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName, nil];

NSMutableAttributedString *resultStrWithBold = [[NSMutableAttributedString alloc]initWithString:str];

[resultStrWithBold setAttributes:dict range:rangeToSubString];

 yourLabel.attributedText = resultStrWithBold;
Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
-1

You can use the following piece of code:

- (void)viewDidLoad {
[super viewDidLoad];
NSString *str = @"Greetings from Capt. Ashim Mittra ,​ Vice President – Flight Operations";
NSString *fromString = @"from";
NSString *toString = @",";

NSArray *seperatorArr = [[NSArray alloc] initWithObjects:fromString, toString, nil];
NSString *reqStr = [self extractSubstringFrom:str seperatedBy:seperatorArr];
}

- (NSString *)extractSubstringFrom:(NSString *)string seperatedBy:(NSArray *)seperatorArray {

NSString *resultingString = string;

for (int i = 0; i < seperatorArray.count; i++) {
    NSArray *newStrArr = [resultingString componentsSeparatedByString:[seperatorArray objectAtIndex:i]];
    if (i == seperatorArray.count - 1) {
        resultingString = [newStrArr firstObject];
    }
    else
        resultingString = [newStrArr lastObject];
}
NSLog(@"Resulting String = %@",resultingString);
return resultingString;

}

Siddharth Sunil
  • 233
  • 2
  • 10