OC之NSString/NSMutableString

简介:

NSString 不可变字符串

NSMutableString 可变字符串

定义:

NSString *str = [[NSString alloc] init];

NSString *str = [[[NSString alloc] init] autorelease];  

注意:在NSString 中存在自己的实例化和初始化的方法 例如:

NSString *str1 = [NSString stringWithCString:"new String" enconding: NSACIIStringEncoding];

NSString *str2 = [NSString alloc] initWithCString:"new String" enconding: NSACIIStringEncoding];

str1str2两个对象是相同的。

NSStringEncoding 中常用的字符编码 

     NSASCIIStringEncoding

     NSUTF8StringEncoding

     NSUnicodeStringEncoding

NSString创建实例 

带“@”符号的方法只能定义含有英文和数字NSString实例,例如:

NSString *str = "Hello money~";

生成含有中文的NSString方法:

1)此方法自动释放内存

NSString *string = [NSString stringWithCString:

"您好" encoding:NSUTF8StringEncoding];  

2)进行alloc后进行初始化

NSString *string = [[NSString alloc] initWithCString:

"您好" encoding:NSUTF8StringEncoding];

使用格式创建字符串:

+ (id)stringWithFormat:(NSString *)format...

- (id)initWithFormat:(NSString *)format...

例如:

NSString *str = "hello";

NSString *string = [NSString stringWithFormat:@"%@ world",str];

NSLog(string); 结果:hello world

常用的替换符:

%@ NSString实例

%d,%D,%i 整数

%u,%U 无符号整数

%x 将无符号整数以十六进制小写字母显示

%X 将无符号整数以十六进制大写字母显示

%f 小数

%c 字符

%s C语言字符串

%% 显示%字符本身

字符串连接,插入和删除 

1、连接

- (NSString *)stringByAppendingString:(NSString *)string;

- (NSString *)stringByAppendingFormat:(NSString *)format...;

例如:

     NSString *str1 = @"hello";

     NSString *str2 = @"world";

     NSString *str3 = [str1 stringByAppendingString:str2];

     NSString *str4 = [str2 stringByAppendingFormat:

@"%d...%d",10,20];

     str4 --> world 10...20

     NSMutableString的生成:

     NSString  + (id)string;  //生成空字符串的实例

     + (id)stringWithString:(NSString *)string; //带自动释放内存

     - (id)initWithString:(NSString *)string;

     例如:

      NSMutableString *string =

[NSMutableString stringWithString:@"hello"];

2、追加字符串

     NSMutableString

     + (void)appendString:(NSString *)string;

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

     例如:

     NSMutableString string = [NSMutableString string];

     [string appendString:@"hello"];

     [string appendString:@"money"];

     [string appendString:@" and world"];

3、插入字符串

     NSMutableString

     + (void)insertString:(NSString *)string atIndex:(unsigned)index;

     index位置插入字符串

     例如:

      NSMutableString *string =

[NSMutableString stringWithString:@"Mac X"];

      [string insertString:@"OS" atIndex:4];

      string --> Mac OS X

4、删除字符串

      NSMutableString

      + (void)deleteCharactersInRange:(NSRange)range;

 例如:

      NSMutableString *myString =

[NSMutableString stringWithString:@"Mac os"];

      [myString deleteCharactersInRange:NSMakeRanger(0,1)];

等价于:

      NSMutableString * myString =

[NSMutableString stringWithString:@"Mac os"];

NSRange range = [myString rangeofString:@”M”];

[myString deleteCharactersInRange:range];

之后myString 就变成了ac os;

5、字符串比较

     NSString

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

6、比较前置串和后置串

     NSString

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

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

     例如:

     NSString *str1 = @"Mac OS";

     NSString *str2 = @"Mac Pro";

     BOOL flag;

     flag = [str1 hasPrefix:@"Mac"];  YES

     flag = [str2 hasSuffix:@"OS"];  NO

7、字符串检索

     如果找到就返回范围,否则NSRangelocation项被设置为NSNotFound

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

 

     - (NSRange)rangeOfString:(NSString *)subString

 option:(unsigned)mask;

 

     - (NSRange)rangeOfString:(NSString *)subString

option:(unsigned)mask  range:(NSRange)range;                                                                                       

mask常用选项列表:

     NSCaseInsensitiveSearch   不区分字母大小写

     NSLiteralSearch  对字符串进行字节单位的比较,一般可提高检索速度

     NSBackwardsSearch  从范围的末尾开始检索

     NSAnchoredSearch  仅检索制定范围的前部。忽略字符串中间的检索字符

     例如:

     NSString *string = @"hello world";

     NSRange range = [string rangeOfString:@"he"];

     if(range.location != NSNotFound)

     {

          NSLog(@" location=%d,length=%d",range.location,range.length);

     }

8、截取字符串

1)返回字符串开头至index位的字符串 不包含索引位

     - (NSString *)substringToIndex:(unsigned)index;

2)返回字符串第index位至末尾的字符串 包含索引位

- (NSString *)substringFromIndex:(unsigned)index;

3)返回字符串中范围range内的字符串 包含索引位

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

例如:

     NSString *string = [string substringWithRange:NSMakeRange(5,2)];

9、读取文本文件

     NSString

     + (id)stringWithContentsOfFile:(NSString *)path

usedEncoding:(NSStringEncoding *)enc error:(NSError **)error     //自动释放内存

   

 - (id)initWithContentsOfFile:(NSString *)path

encoding:(NSStringEncoding)enc error:(NSError **)error

     例如:

      NSString *string = [NSString stringWithContentsOfFile

:@"/user/test/LFF.txt" encoding:NSUTF8StringEncoding error: nil]

还有一个更强大的方法:

//NSURL *url = [[NSURL alloc] initWithString:

@"file:///Users/apple/Desktop/1.txt"];

NSURL *url = [NSURL fileURLWithPath:

@"/Users/apple/Desktop/1.txt"];

   

NSString *s6 = [[NSString alloc] initWithContentsOfURL:url encoding:

NSUTF8StringEncoding error:nil];

NSLog(@"s6=\n%@", s6);

使用读取URL的方法,不仅可以读取本地资源,还可以读取网络资源,但要注意资源路径的格式:

URL : 资源路径

    协议头://路径

    file://

    ftp://

10、将字符串写入文本文件

     NSString

    -(BOOL)writeToFile:(NSString*)path atomically:

(BOOL)useAuxiliaryFile encoding: (NSStringEncoding)

enc error:(NSError **)error

例如:[@"Jack\nJack" writeToFile:@"/Users/apple/Desktop/my.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];


目录
相关文章
|
Windows
NSString的boolValue方法甚解
前言 NSString的boolValue之前有使用,但是一直没有真正了解什么时候返回YES(true)或NO(false)。其实,苹果在官方文档中已经写的很清楚,按command + control 点击boolValue进入文档就可以看到: boolValue The Boolean value of the string.
961 0
|
存储 对象存储 块存储
|
机器学习/深度学习 C语言 索引