查找字符位置的方法
- (NSRang) rangOfString:(NSString *)aString;
截取该位置之前的字符串方法
- (NSString *)subStringToIndex:(NSInteger)anIndex;
截取该位置之后的字符串方法(不包括该位置的字符)
- (NSString *) subStringFromIndex:(NSInteger)anIndex;
将字符串转换为数字类型的方法如int
NSString *numberString = @"123";
int number = [numberString intValue];
将数字转化为字符串方法
int number = 123;
NSString *numberString = [NSString stringWithFormat:@"%d",number];
示例代码:
- (NSString *)operation:(NSString *)text
{
NSRange range = [text rangeOfString:@"+"];
if(range.location != NSNotFound) {
NSInteger index = range.location;
NSString *firstNumber = [text substringToIndex:index];
NSString *finalNumber = [text substringFromIndex:index + 1];
int theFirstNumber = [firstNumber intValue];
int theFinalNumber = [finalNumber intValue];
int theResultNumber = theFirstNumber + theFinalNumber;
NSString *result = [NSString stringWithFormat:@"%d",theResultNumber];
return result;
} else {
return text;
}
}