1.取出符串“123-456-789-000”中的数字部分,组成一个新的字符串输出
1 NSString *str=[[NSString alloc]initWithFormat:@"123-456-789-000"]; 2 3 //(1)把”-“去掉,并放在数组内 并输出 4 NSArray *arr=[str componentsSeparatedByString:@"-"]; 5 NSLog(@"%@%@%@%@",[arr objectAtIndex:0],[arr objectAtIndex:1],[arr objectAtIndex:2],[arr objectAtIndex:3]); 6 //(2)用空字符代替“-“ 7 NSString *newStr=[str stringByReplacingOccurrencesOfString:@"-" withString:@""]; 8 NSLog(@"%@",newStr);
2.放四个十进制三位数到一个数组中,然后按从小到大排序后组成一个新的数组
#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { NSMutableArray *arr=[[NSMutableArray alloc]initWithObjects:@"123",@"234",@"110",@"142", nil]; for(int i=0;i<[arr count]-1;i++){ for(int j=0;j<[arr count]-i-1;j++){ NSComparisonResult result=[[arr objectAtIndex:j] compare:[arr objectAtIndex:j+1]]; if(result==NSOrderedDescending){ [arr exchangeObjectAtIndex:j+1 withObjectAtIndex:j]; } } } for(int i=0;i<[arr count];i++){ NSLog(@"%@",[arr objectAtIndex:i]); } } return 0; }
时间: 2024-10-05 05:04:46