NSString常用方法
- 定义:
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];
str1 和 str2 两个对象是相同的。
- NSStringEncoding 中常用的字符编码 :
NSASCIIStringEncoding
NSUTF8StringEncoding
NSUnicodeStringEncoding
- 生成含有中文的 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];
- 1、连接
- (NSString *)stringByAppendingString:(NSString *)string;
- (NSString *)stringByAppendingFormat:(NSString *)format...;
- 追加字符串 NSMutableString
+ (void)appendString:(NSString *)string;
- (void)appendFormat:(NSString *)format...;
- 3、插入字符串 NSMutableString
+ (void)insertString:(NSString *)string atIndex:(unsigned)index; 从 index 位置插入字符串
- 4、删除字符串 NSMutableString
+ (void)deleteCharactersInRange:(NSRange)range;
- 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、字符串检索
如果找到就返回范围,否则 NSRange 的 location 项被设置为 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];