国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > 综合技术 > Object-C学习札记 NSString常用方法

Object-C学习札记 NSString常用方法

来源:程序员人生   发布时间:2015-04-25 09:47:21 阅读次数:3728次
--实例化方法--------------
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创建实例----------------
带“@”符号的方法只能定义含有英文和数字的NSString实例,例如:
NSString *str = "Hello money~";
--生成含有中文的NSString方法-------------
//此方法自动释放内存
+ (id)stringWithCString:(const char*)cString encoding:(NSStringEncoding)encoding;
//进行alloc落后行初始化
- (id)initWithCString:(const char*)cString encoding:(NSStringEncoding)encoding;
例如:
NSString *string = [NSString stringWithCString:"您好" encoding:NSUTF8StringEncoding];
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 将无符号整数以106进制小写字母显示
%X 将无符号整数以106进制大写字母显示
%f 小数
%c 字符
%s C语言字符串
%% 显示%字符本身

--------------------------
NSRange

--NSRange的定义
typedef struct _NSRange
{
     unsigned int location;
     unsigned int length;
}NSRange;

NSMakeRange函数
--这个函数比较特殊 返回1个NSRange的对象。
NSMakeRanger(unsigned int location,unsigned int length);
例如:
NSRange range = NSMakeRanger(0,5);
NSLog(@"location is %d,length is %d",range.location,range.length);

---------------------------
计算字符串长度
- (unsigned int)length;

---------------------------
字符串联接,插入和删除
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 *string = [NSMutableString stringWithString:@"Mac os"];
     [string deleteCharactersInRange:NSMakeRanger(0,1)];
     NSLog(string);
     string -->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、字符串检索
     NSString
     //如果找到就返回范围,否则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          对字符串进行字节单位的比较,1般可提高检索速度
     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、截取字符串
     NSString
     - (NSString *)substringToIndex:(unsigned)index;     //返回字符串开头至index位的字符串 不包括索引位
     - (NSString *)substringFromIndex:(unsigned)index; //返回字符串第index位至末尾的字符串 包括索引位
     - (NSString *)substringWithRange:(NSRange)range;     //返回字符串中范围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/yw.txt" encoding:NSUTF8StringEncoding error:&error];
     if(string){}
10、输出文本文件
     NSString
     - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError **)error
     //参数 atomically 暂时将文件保存到辅助文件中
     //path
     The file to which to write the receiver. If path contains a tilde (~) character, you must expand it with stringByExpandingTildeInPath before invoking this method.
    ----下面是网上找的例子 感谢 @chenshizero
    //扩大路径
    NSString *Path = @"~/NSData.txt";
    NSString *absolutePath = [Path stringByExpandingTildeInPath];
    NSLog(@"absolutePath:%@",absolutePath);
    NSLog(@"Path:%@",[absolutePath stringByAbbreviatingWithTildeInPath]);

    //文件扩大名
    NSString *Path = @"~/NSData.txt";
    NSLog(@"Extension:%@",[Path pathExtension]);
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生