首页 > 代码库 > NSString中方法的使用

NSString中方法的使用

NSString的使用。

1、 - (NSUInteger)length; 返回字符串的长度

NSString *demo = @"hello world";

NSString *demo two=@"good morning";

NSLog(@"%ld", [demo length]);
输出为 11

2 - (unichar)characterAtIndex:(NSUInteger)index; 返回在字符串中的某个位置的字符
NSLog(@"%C", [demo characterAtIndex: 2]);
 输出为2;

3、- (void)getCharacters:(unichar *)buffer range:(NSRange)aRange; 截取某段字符到buffer中

定义buffer不用*

uichar buffer[12];

4- (NSString *)substringFromIndex:(NSUInteger)from;  截取指定位置后面的字符串
NSLog(@"%@", [demo substringFromIndex:3]);
输出为 lo world

5、- (NSString *)substringToIndex:(NSUInteger)to;  截取指定长度的字符串,从索引0开始
NSLog(@"%@", [demo substringToIndex:3]);
输出为  hel

6、- (NSString *)substringWithRange:(NSRange)range;   截取字符串指定段,返回类型NSString
NSLog(@"%@", [demo substringWithRange:NSMakeRange(2, 3)]);
输出为 llo

7、- (NSComparisonResult)compare:(NSString *)string;   比较字符串
NSString *demoTwo = @"good morning";
NSLog(@"%ld", [demo compare:demoTwo]); 

 比较字符输出为 因为H比G大  所以返回为1;

8、- (NSComparisonResult)caseInsensitiveCompare:(NSString *)string; 不区别大小比较

NSLog(@"%ld", [demoTwo caseInsensitiveCompare:@"Good Morning"]);
输出为 0

9、- (NSComparisonResult)localizedCompare:(NSString *)string; 本地化比较
NSLog(@"%ld", [demoTwo localizedCompare:@"大家早"]);
输出为 -1

10、- (BOOL)isEqualToString:(NSString *)aString; 测试两个字符串是否相等
NSLog(@"%d", [demoTwo isEqualToString:@"Good Morning"]);
输出为 0

 

12、- (BOOL)hasPrefix:(NSString *)aString;  测试字符串是否以 nsstring 开始

 

NSLog(@"%d", [demoTwo hasPrefix:@"good"]);
2013-02-27 19:17:55.088 NSStringDemo[766:303] 1
 
13、- (BOOL)hasSuffix:(NSString *)aString;  测试字符串是否以 nsstring 结尾
NSLog(@"%d", [demoTwo hasSuffix:@"morning"]);
输出为  1

 

14、- (NSRange)rangeOfString:(NSString *)aString; 搜索字符串One是否存在于字符串Two
NSLog(@"%ld", [demoTwo rangeOfString:@"morning"].location);
输出为  5

15、- (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask; 以某些限制条件搜索字符串One是否存在于字符串Two
NSLog(@"%ld", [demoTwo rangeOfString:@"Morning" options:NSCaseInsensitiveSearch].location);
输出为  5


16、- (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask range:(NSRange)searchRange; 以某些限制条件搜索字符串One是否存在于字符串Two指定位置段
NSLog(@"%ld", [demoTwo rangeOfString:@"Mo" options:NSCaseInsensitiveSearch range:NSMakeRange(4, 3)].location);
输出为 5

23、- (NSString *)stringByAppendingString:(NSString *)aString; 将字符串One添加字符串Two后面
NSLog(@"%@", [demoTwo stringByAppendingString:@" too !"]);
输出为  good morning too !

24、- (NSString *)stringByAppendingFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2); 将多个字符串添加字符串Two后面

NSLog(@"%@", [demoTwo stringByAppendingFormat:@"and int is %d,string is %@--, %@--,%@--", 3, @"One", @"Two", @"Three"]);
输出为  good morningand int is 3,string is One--, Two--,Three--

25、类型转化- (double)doubleValue; 返回转化的double类型
- (float)floatValue;  返回转化的float类型
- (int)intValue;  返回转化的int类型
- (NSInteger)integerValue NS_AVAILABLE(10_5, 2_0);  返回转化的NSInteger(

32位系统NSInteger是一个int,即32位,但当时64位系统时,NSInteger便是64位的)类型

- (long long)longLongValue NS_AVAILABLE(10_5, 2_0); 返回转化的 长int类型
- (BOOL)boolValue NS_AVAILABLE(10_5, 2_0);   返回转化的 长BOOL类型

NSString *demoThree = @"3.343demo";
NSLog(@"%f", [demoThree doubleValue]);
NSLog(@"%f", [demoThree floatValue]);
NSLog(@"%d", [demoThree intValue]);
NSLog(@"%ld", [demoThree integerValue]);
NSLog(@"%ld", [demoThree longLongValue]);
NSLog(@"%d", [demoThree boolValue]);

输出为
3.343000
3.343000
3
3
3
1

26.NSString *demoFour = [[NSString alloc] initWithString:@"One, Two, Three, Four, Five"];字符串转化为数组
NSLog(@"%@", [[demoFour componentsSeparatedByString:@","] objectAtIndex: 3]);
输出为 Four

27、- (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator NS_AVAILABLE(10_5, 2_0);  依据字符编码,分割字符串
NSLog(@"%@", [[demoFour componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]] objectAtIndex:2]);
输出为  Three