我有一个字符串NSString *[email protected]"88) 12-sep-2012";
或[email protected]"8) blablabla";
查找字符的索引在字符串
我想只打印前的字符“)”,因此如何找到字符的索引“)”。或者我该怎么做?
在此先感谢。
我有一个字符串NSString *[email protected]"88) 12-sep-2012";
或[email protected]"8) blablabla";
查找字符的索引在字符串
我想只打印前的字符“)”,因此如何找到字符的索引“)”。或者我该怎么做?
在此先感谢。
NSString *str = [[yourString componentsSeparatedByString:@")"] objectAtIndex:0];
您能给我们下面的代码,看看之前字符 “)”
// 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]);
NSUInteger index = [Original rangeOfString:@")"];
NSString *result = [Original substringWithRange:NSMakeRange(0, index)];
U可以找到人物的指数 “)” 是这样的:
NSString *[email protected]"88) 12-sep-2012";
NSRange range = [Original rangeOfString:@")"];
if(range.location != NSNotFound)
{
NSString *result = [Original substringWithRange:NSMakeRange(0, range.location)];
}
另一个解决方案:
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);
尝试下面的代码以获取特定字符的索引中的字符串: -
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];
请接受答案如果你已经解决了你的问题 –