如何正确获取用户输入的字符串

2015-12-15  本文已影响113人  飞雨2013

- (void)viewDidLoad {

[super viewDidLoad];

NSArray * array = @[@"123",[NSNull null],@"  23  45    ",@"    456  "];

int count = 0;

for (NSString * str in array) {

/*

[NSNull null]作为占位符使用在容器中“字典,数组,集合”

检测参数时,先去除字符串两端的空格,再将字符串中间的空格@" "替换掉以@""就是我们要得到的字符串

*/

if (![str isEqual: [NSNull null]]) {

count ++;

NSLog(@"count = %d-%@",count,[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]);

/*

2015-12-15 11:38:32.238 试炼[1328:64039] count = 1-123

2015-12-15 11:38:32.239 试炼[1328:64039] count = 2-23  45

2015-12-15 11:38:32.239 试炼[1328:64039] count = 3-456

*/

//            NSLog(@"str = %@",[str stringByReplacingOccurrencesOfString:@" " withString:@""]);

/*

2015-12-15 11:37:59.011 试炼[1316:63485] str = 123

2015-12-15 11:37:59.012 试炼[1316:63485] str = 2345

2015-12-15 11:37:59.012 试炼[1316:63485] str = 456

*/

}

}

}

上一篇 下一篇

猜你喜欢

热点阅读