2012-09-12 137 views

回答

12
NSString *str = [[yourString componentsSeparatedByString:@")"] objectAtIndex:0]; 
4

您能给我们下面的代码,看看之前字符 “)”

// this would split the string into values which would be stored in an array 
    NSArray *splitStringArray = [yourString componentsSeparatedByString:@")"]; 
    // this would display the characters before the character ")" 
    NSLog(@"%@", [splitStringArray objectAtIndex:0]); 
2
NSUInteger index = [Original rangeOfString:@")"]; 

NSString *result = [Original substringWithRange:NSMakeRange(0, index)]; 
7

U可以找到人物的指数 “)” 是这样的:

NSString *[email protected]"88) 12-sep-2012"; 
NSRange range = [Original rangeOfString:@")"]; 
if(range.location != NSNotFound) 
{ 
NSString *result = [Original substringWithRange:NSMakeRange(0, range.location)]; 
} 
1

另一个解决方案:

NSString *[email protected]"88) 12-sep-2012"; 
NSRange range = [Original rangeOfString:@")"]; 
NSString *result = Original; 

if (range.location != NSNotFound) 
{ 
    result = [Original substringToIndex:range.location]; 
} 

NSLog(@"Result: %@", result); 
3

尝试下面的代码以获取特定字符的索引中的字符串: -

NSString *string = @"88) 12-sep-2012"; 
NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:@")"]; 
NSRange range = [string rangeOfCharacterFromSet:charSet]; 

if (range.location == NSNotFound) 
{ 
    // ... oops 
} 
else { 
    NSLog(@"---%d", range.location); 
    // range.location is the index of character) 
} 

,并获得)字符使用前字符串这样的: -

NSString *str = [[string componentsSeparatedByString:@")"] objectAtIndex:0];