2016-08-01 2 views
0

@"Greetings from Capt. Ashim Mittra,​ Vice President – Flight Operations"과 같은 문자열이 있는데 "Capt. Ashim Mittra ". 나는 "에서"단어에서 시작 ","(쉼표)객관적인 C에서 동적 문자열에서 하위 문자열을 추출하는 방법은 무엇입니까?

+1

표시 코드입니다. –

+0

'substringWithRange'와'rangeOfString :'을 사용할 수 있습니다. "대담한"UI의 경우에는 다릅니다. 굵은 글씨가 있거나 없거나'UILabel' /'UITextView'의'font' 속성을 사용하면'NSAttributedString'을 사용할 수 있습니다 ... – Larme

+0

정규 표현식을 사용하는 것은 어떻습니까? – Eiko

답변

0

,

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); 

당신이 당신의 레이블 또는 다른 당신이 굵은 부분 뭔가 등으로 텍스트를 표시 할 때에 의한 텍스트를 설정할 수 있습니다

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; 
1

사용하려면이 코드를 읽을 수 즉 :

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]; 

가 굵은 사용이를 만들려면을; 당신이 좋아하는 뭔가를 할 수

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

아니요 오류 검사! – Droppy

2

정규 표현식을 사용하여 이름을 찾을 수 있습니다. 예 :

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); 

         }]; 

이것은 아마도 약간 개선되어야 할 것입니다. 그러나 올바른 조정을 위해 모든 입력 데이터를 점검해야합니다. 입력 문자열에 여러 이름이 있습니다.

-1

당신은 다음 코드 조각을 사용할 수 있습니다

- (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; 

} 이렇게하는 것을 시도했다

관련 문제