首页 > 代码库 > OC语言--字符串(NSString& NSMutableString)

OC语言--字符串(NSString& NSMutableString)

首先声明一下:

OC 中的函数,是全局的,如 NSLog();,它不属于任何类,不属于任何一个对象。

OC 中的方法,非全局的,init 方法,用中括号来使用,它属于类本身,或者属于对象本身;分为类方法,和实例方法(对象方法);方法被封装对象,或类模板当中

1. 不可变字符串NSString

 

1.1 OC字符串和C字符串的不同

OC字符串是对象,占用堆空间;C 字符串是数据,它占用栈空间

 

1.2 OC字符串的常见操作

(1) 常用创建方法

//实例方法创建字符串

- (instancetype)initWithString:(NSString *)aString;

- (instancetype)initWithFormat:(NSString *)format, …;

- (instancetype)initWithUTF8String:(const char *)bytes;

 

//用类方法创建字符串

+ (instancetype)stringWithString:(NSString *)aString;

+ (instancetype)stringWithFormat:(NSString *)format,, …;

+ (instancetype)stringWithUTF8String:(const char *)bytes;

 

(2)求字符串长度

- (NSUInteger)length;

 

(3) 通过索引获取相应的字符

- (unichar)characterAtIndex:(NSUInteger)index;

 

(4) 字符串比较

//判断两个字符串是否相等 

- (BOOL)isEqualToString:(NSString *)aString;

 

//比较两个字符串大小

- (NSComparisonResult)compare:(NSString *)aString;

 

//不区分大小写比较大小

- (NSComparisonResult)caseInsensitiveCompare:(NSString *)aString;

 

(5) 字符串查找//在给定的字符串中去查找子字符串

- (NSRange)rangeOfString:(NSString *)aString;

 

(6) 判断前后缀

//前缀字符串

- (BOOL)hasPrefix:(NSString *)aString;

//后缀字符串

- (BOOL)hasSuffix:(NSString *)aString;

 

(7) 数字字符串转化为各种数字类型

- (double)doubleValue;

- (float)floatValue;

- (int)intValue;

//long unsigned int

- (NSInteger)integerValue;

- (long long)longLongValue;

- (BOOL)boolValue;  

 

(8) 大小写转换

- (NSString *)uppercaseString;//将所有字符转换成大写

- (NSString *)lowercaseString;//将所有字符转换成小写

- (NSString *)capitalizedString;////将第一个字符转换成大写

 

*(9)字符串提取

- (NSString *)substringFromIndex:(NSUInteger)from;

- (NSString *)substringToIndex:(NSUInteger)to;

- (NSString *)substringWithRange:(NSRange)range;

 

*(10) 从本地读取文件内容

- (id)initWithContentsOfFile:(NSString *)path 

encoding:(NSStringEncoding)enc 

error:(NSError **)error;

 

+ (id)stringWithContentsOfFile:(NSString *)path 

encoding:(NSStringEncoding)enc 

error:(NSError **)error;

 

eg.NSString基本使用

#import <Foundation/Foundation.h>int main(int argc, const char * argv[]){  @autoreleasepool {    NSString* a = [[NSString alloc]initWithString:@"你好吗,hello"];    NSString* aa = [NSString stringWithFormat:@"%d %@",20,@"world"];    NSMutableString* b = [[NSMutableString alloc]initWithString:@"HELLO"];    unichar myChar = [a characterAtIndex:2];        NSLog(@"The Length:%lu",[a length]);    NSLog(@"The Length:%lu",[aa length]);    NSLog(@"The Length:%lu",[b length]);        NSLog(@"The character: %C",myChar);        NSString* aaa = a;    //判断是否相同    if([a isEqualToString:aaa] == YES){      printf("a isEqualToString aaa\n");    }else{      printf("a is different from aaa\n");    }    //比较字符串大小    if([a caseInsensitiveCompare:aa] == NSOrderedSame){      printf("a equals aaa\n");    }else if ([a caseInsensitiveCompare:aa] == NSOrderedAscending){      printf("a less than aaa\n");    }else{//NSOrderedDescending      printf("a great than aaa\n");    }      }    return 0;}

 

 

2 可变字符串NSMutableString

 

2.1 什么是可变字符串

可变字符串对象的内容可以改变

 

2.2 NSMutableString 和NSString 的关系

NSMutableString 继承于 NSString,

NSString类的所有方法NSMutableString都可以使用

 

2.3 可变字符串常见操作

 

(1) 指定索引插入子串

  - (void)insertString:(NSString *)aString 

atIndex:(NSUInteger)loc;

 

(2) 删除指定范围的子串

- (void)deleteCharactersInRange:(NSRange)range;

 

(3) 追加子串

- (void)appendString:(NSString *)aString;

 

(4) 追加格式化子串

- (void)appendFormat:(NSString *)format, ...;

 

(5) 修改子串

- (void)setString:(NSString *)aString;

 

eg.NSMutableString基本使用

#import <Foundation/Foundation.h>int main(int argc, const char * argv[]){  @autoreleasepool {    NSString* a = [[NSString alloc]initWithString:@"HELLO"];    NSString* sub = [[NSString alloc]initWithString:@"EL"];        NSRange myRange = [a rangeOfString:sub];        printf("myRange.location:%lu\n",myRange.location);    printf("myRange.length:%lu\n",myRange.length);          //前缀存在判断      //NSString* b = [NSString stringWithString:a];    if ([a hasPrefix:@"HEL"]) {      printf("has Prefix.\n");    }else{      printf("has not Prefix.\n");    }          //数字字符串转换成相应的数字类型    NSString* c = [NSString stringWithUTF8String:"654321"];    NSLog(@"Number:%d",[c intValue]+100);      //- (BOOL)boolValue;使用    NSString* d = [NSString stringWithFormat:@"no"];    NSLog(@"%s",[d boolValue]?"yes":"no");      //- (NSString *)uppercaseString;      //将所有字符转换成大写    NSLog(@"%@",[d uppercaseString]);      //- (NSString *)lowercaseString;      //将所有字符转换成小写    NSLog(@"%@",[d lowercaseString]);      //- (NSString *)capitalizedString;      //将第一个字符转换成大写    NSLog(@"%@",[d capitalizedString]);      //-----------------------------    NSString* longString = [NSString stringWithFormat:@"ABCDEFG1234567890"];      //   - (NSString *)substringFromIndex:(NSUInteger)from;    NSLog(@"%@",[longString substringFromIndex:6]);      //- (NSString *)substringToIndex:(NSUInteger)to;  //substringToIndex不包含index位置的字符本身    NSLog(@"%@",[longString substringToIndex:6]);      //- (NSString *)substringWithRange:(NSRange)range;    NSRange myRange2 = {5,6};    myRange2.location = 4;    myRange2.length = 7;    NSLog(@"%@",[longString substringWithRange:myRange2]);      }    return 0;}

 

OC语言--字符串(NSString& NSMutableString)