设为首页 加入收藏

TOP

[Objective-C]OC中字符串(NSString)的基本概念和常用处理方法(二)
2014-11-23 17:55:20 来源: 作者: 【 】 浏览:16
Tags:Objective-C 字符串 NSString 基本 概念 常用 处理 方法
ding; + (id)stringWithCString:(const char *)cString encoding:(NSStringEncoding)enc; /* These use the specified encoding. If nil is returned, the optional error return indicates problem that was encountered (for instance, file system or encoding errors). */ - (id)initWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error; - (id)initWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error; + (id)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error; + (id)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error; /* These try to determine the encoding, and return the encoding which was used. Note that these methods might get "smarter" in subsequent releases of the system, and use additional techniques for recognizing encodings. If nil is returned, the optional error return indicates problem that was encountered (for instance, file system or encoding errors). */ - (id)initWithContentsOfURL:(NSURL *)url usedEncoding:(NSStringEncoding *)enc error:(NSError **)error; - (id)initWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc error:(NSError **)error; + (id)stringWithContentsOfURL:(NSURL *)url usedEncoding:(NSStringEncoding *)enc error:(NSError **)error; + (id)stringWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc error:(NSError **)error; 6:求字符串的长度

        //求字符串的长度
        NSString  *string12=[[NSString alloc]initWithString:@"abcd"];
        NSLog(@"string2的长度为:%ld",[string12 length]);

7:字符串的转换

//字符串的转换

NSString *string13=[[NSString alloc]initWithString:@"HelloWorld"];
        NSLog(@"upper %@",[string13 uppercaseString]);
        NSLog(@"lower %@",[string13 lowercaseString]);
        NSLog(@"capitalized %@",[string13 capitalizedString]);
8:字符串转换成基本数据类型
 




//字符串转成基本数据类型 NSString *string14=[[NSString alloc]initWithString:@"3.14"]; NSLog(@"%.2f\n",[string14 floatValue]); 
9:字符串转换成数组
  




//字符串转换成数组 NSString *string15=[[NSString alloc]initWithString:@"abc bcd xyz"]; NSArray *array=[string15 componentsSeparatedByString:@""]; NSLog(@"%@\n",array); 
10:字符串的截取

 
 
//截取字符串 NSString *string16=@"abcdefg"; NSLog(@"%@",[string16 substringToIndex:2]); NSLog(@"%@",[string16 substringFromIndex:2]); NSRange range; range.location=2; range.length=3; NSLog(@"%@",[string16 substringWithRange:range]); 
11:字符串的拼接:
   


//字符串的拼接 NSString *string17=@"abc"; NSString *string18=@"xyz"; NSString *appString=[[NSString alloc]initWithFormat:@"这是拼接的字符串:%@ and %@",string17,string18]; NSString *appString2=[string17 stringByAppendingString:@"123"]; NSString *appstring3=[string17 stringByAppendingFormat:@"%@",string18]; NSLog(@"%@,%@,%@",appString,appString2,appstring3);
12:字符串的查找:
  
//字符串的查询 NSString *link=@"abcdffe-===fefjfwfw"; NSRange range1=[link rangeOfString:@"abcd"]; NSLog(@"%@",NSStringFromRange(range1)); if(range1.location != NSNotFound){ NSLog(@"found!"); }

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇[Objective-C]OC中引用计数和对象.. 下一篇[Objective-C]OC中可变字符串(NSM..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: