iOS

NSCharacterSet --字符串的一些用法

2016-07-01  本文已影响79人  跬步千里_LenSky

首先自己定义一个NSCharacterSet, 包含需要去除的特殊符号

//去特殊字符

NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"@/:;()¥「」"、[]{}#%-*+=_\\|~<>$€^•'@#$%^&*()_+'\""];

//大小写转换

使用 lowercaseString,uppercaseString

- (void)test{

NSString *testString = @"Hello World";

// lower case

NSString *lowerCaseString1 = [testString lowercaseString];

NSString *lowerCaseString2 = testString.lowercaseString;

// upper case

NSString *upperCaseString1 = [lowerCaseString1 uppercaseString];

NSString *upperCaseString2 = lowerCaseString1.uppercaseString;

NSLog(@"lowerCaseString1: %@",lowerCaseString1);

NSLog(@"lowerCaseString2: %@",lowerCaseString2);

NSLog(@"upperCaseString1: %@",upperCaseString1);

NSLog(@"upperCaseString2: %@",upperCaseString2);

// capitalize the first word of the sentence

NSString *txt = @"hi my friends!";

txt = [txt stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[txt substringToIndex:1] uppercaseString]];

NSLog(@"txt: %@", txt);

}

输出结果

2014-08-26 17:00:30.266 MyApp[68821:60b] lowerCaseString1: hello world

2014-08-26 17:00:30.267 MyApp[68821:60b] lowerCaseString2: hello world

2014-08-26 17:00:30.267 MyApp[68821:60b] upperCaseString1: HELLO WORLD

2014-08-26 17:00:30.267 MyApp[68821:60b] upperCaseString2: HELLO WORLD

2014-08-26 17:00:30.268 MyApp[68821:60b] txt: Hi my friends!

参考来源

http://www.tuicool.com/articles/BVFFNba

substringFromIndex表示从指定位置开始截取字符串到最后,所截取位置包含该指定位置。

十六进制转十进制数值  

NSString *str = @"11";

unsigned int r;

[[NSScanner scannerWithString:str] scanHexInt:&r];

NSLog(@"------RRR---------%d",r);

输出结果------RRR---------17 

上一篇下一篇

猜你喜欢

热点阅读