今天做练习,没找到最后一天练习的答案,自己做了一份,结果没什么错,做个参考吧
- //1.求字符串“158”和“39”按十进制数值做差后的结果以字符串形式输出
- #import <Foundation/Foundation.h>
- int main() {
- @autoreleasepool
- {
- NSString *s1 = @"158";
- NSString *s2 = @"39";
- int num1 = (int)[s1 integerValue];
- int num2 = (int)[s2 integerValue];
- int r = num1 - num2;
- NSLog(@"%@",@(r));
- }
- return 0;
- }
复制代码
- //2.取出符串“123-456-789-000”中的数字部分,组成一个新的字符串输出
- #import <Foundation/Foundation.h>
- int main() {
- @autoreleasepool
- {
- NSString *s = @"123-456-789-000";
- NSArray *array = [s componentsSeparatedByString:@"-"];
- NSMutableString *s1 = [[NSMutableString alloc] init];
- for (id number in array)
- {
- [s1 appendString:number];
- }
- NSLog(@"%@",s1);
-
- }
- return 0;
- }
复制代码
- //3.实现函数NSString * stringWithoutNum(NSString *str);
- //删除字符串str的所有数字
- #import <Foundation/Foundation.h>
- NSString * stringWithoutNum(NSString *str);
- int main() {
- @autoreleasepool
- {
- NSString *s1 = @"vever5t54e3wfe54t540";
- NSString *s2 = stringWithoutNum(s1);
- NSLog(@"\ns1:%@\ns2:%@",s1,s2);
- }
- return 0;
- }
- NSString * stringWithoutNum(NSString *str)
- {
- NSMutableString *s = [str mutableCopy];
- for (int i = 0;i < s.length ;i++)
- {
- unichar c = [s characterAtIndex:i];
- if (isdigit(c))
- {
- [s deleteCharactersInRange:NSMakeRange(i,1)];
- i--;
-
- }
- }
- return [s copy];
- }
复制代码
- //4.实现函数:NSArray *kickOff10(NSArray *array);
- //将数组中的@10去掉
- #import <Foundation/Foundation.h>
- NSArray *kickOff10(NSArray *array);
- int main() {
- @autoreleasepool
- {
- NSArray *array = @[@1,@11,@11,@32,@"10",@11];
- NSArray *a = kickOff10(array);
- NSLog(@"\n%@\n%@",array,a);
- }
- return 0;
- }
- NSArray *kickOff10(NSArray *array)
- {
- NSMutableArray *a1 = [NSMutableArray arrayWithArray:array];
- [a1 removeObject:@10];
- return [a1 copy];
-
- }
复制代码
- //5.将2013年05月05日转换为2013-05-05
- #import <Foundation/Foundation.h>
- int main() {
- @autoreleasepool
- {
-
- NSString *s1 = @"2013年05月05日";
- NSDateFormatter *format = [[NSDateFormatter alloc] init];
- //按照这种格式将字符串转为日期
- format.dateFormat = @"yyyy年MM月dd日";
- NSDate *d = [format dateFromString:s1];
- NSDateFormatter *format2 = [[NSDateFormatter alloc] init];
- format2.dateFormat = @"yyyy-MM-dd";
- //再将上述的日期转成需要的字符串格式输出
- NSString *date = [format2 stringFromDate:d];
- NSLog(@"%@",date);
- }
- return 0;
- }
复制代码 |
|