------ Java培训、Android培训、iOS培训、.Net培训期待与您交流! ------
http://www.itheima.com
1.NSString * str = @"hello";//最直接的方式创建字符串,字符串存在常量区
2.1 NSString * str1 = [[NSString alloc]initWithString:str];//用一个字符串创建另一个字符串
2.2 NSString * str2 = [NSString stringWithString:str];//用一个字符串创建另一个字符串
char a[3]="ab";
4.1 NSString * str3 = [[NSString alloc]initWithUTF8String:a];//把C的字符串转化为OC的字符串
4.2 NSString * str4 = [NSString stringWithUTF8String:a];//把C的字符串转化为OC的字符串
int b = 123;
5.1 NSString * str5 = [[NSString alloc]initWithFormat:@"b = %d", b];//格式化的方式,字符串存在常量区
5.2 NSString * str6 = [ NSString stringWithFormat : @"b = %d" , b ];//格式化的方式,字符串存在堆区1.字符串大小比较(compare方法): - (NSComparisonResult)compare:(NSString *)string;
该方法返回的结果有三种:NSOrderedAscending(升序),NSOrderedSame(降序),NSOrderedDescending(相等);
compare方法常见的参数有三个:NSCaseInsensitiveSearch:不区分大小写字符,NSLiteralSearch:进行完全比较,区分大小写,NSNumericSearch:比较字符串的字符个数,而不是字符值。
//不区分大小写比较两个字符串
NSComparisonResult result = [str1 compare:str2 options:NSCaseInsensitiveSearch];
2.判断字符串是否相等:不可以用 “==” 进行判断,例如NSString *str1 = @"abc";
NSString *str2 = [NSStringstringWithFormat:@"abc"];
str1存放在常量区,str2存放在堆区,它们的地址不同,可内容都是abc,显然用“==” 进行判断是不行的。
判断字符串是否相等,使用 - (BOOL)isEqualToString:(NSString *)aString; 该方法区分大小写。
BOOL isEqual = [str1 isEqualToString:str2];
3.1 NSString前缀检查:- (BOOL)hasPrefix:(NSString *)str;
BOOL isPrefix = [str hasPrefix:@"http"];
3.2 NSString后缀检查:- (BOOL)hasSuffix:(NSString *)str;
3.3 检查字符串内容中是否包含了某字符串:- (NSRange)rangeOfString:(NSString *)searchString;
NSString *str1 =@"hello world";
NSString *str2 =@"llo";
NSRange range = [str1 rangeOfString:str2];
NSLog(@"%lu,%lu",range.location,range.length);//如果查找不到,返回给range.location的值是NSNotFound(最大的long类型的数)。
打印结果为2,3 4.1 字符串的截取:从指定位置from开始(包括指定位置的字符)到尾部。- (NSString *)substringFromIndex:(NSUInteger)from;
- (NSString *)substringToIndex:(NSUInteger)to;
NSString *str =@"anjhgl";
NSRange range = {2,2};
NSString *str = [str substringWithRange:range];
NSLog(@"%@",str);
打印结果为jh
5.字符串的替换:- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement);
str =@"**jkhgfk*iwwss*con";
newStr = [str stringByReplacingOccurrencesOfString:@"*"withString:@"/"];
NSLog(@"%@",newStr);
打印结果为://jkhgfk/iwwss/con
6.统计字符串的长度: @property (readonly) NSUInteger length;NSInteger len = str.length;
unichar a = [str characterAtIndex:1];
NSLog(@"%c",a);
- (float)floatValue; //转为float型。
- (int)intValue;//转为int型。
- (char *)UTF8String;//转为C语言中的字符串。
9.去除字符串首尾的空格:- (NSString *)stringByTrimmingCharactersInSet:(NSCharacterSet *)set;+ (NSCharacterSet *)whitespaceCharacterSet; 例如:
NSString *str = @" nihao ";
NSString *str1=[str stringByTrimmingCharactersInSet:[NSCharacterSet
whitespaceCharacterSet]];
NSLog(@"%<---%@--->",str1);
打印结果:<---nihao--->
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError **)error;
if([str writeToFile:@"/Users/apple/Desktop//stt.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil])
NSLog(@"写入成功");
else
NSLog(@"写入失败");
2.读取文件内容:+ (nullable instancetype)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error;if ([NSString stringWithContentsOfFile:@"/Users/apple/Desktop/stt.txt" encoding:NSUTF8StringEncoding error:nil]) {
NSLog(@"读取成功");
NSLog(@"%@",str);
}else{
NSLog(@"读取失败!");}
NSURL *url = [NSURL URLWithString:@"file:///Users/apple/Desktop/3.txt"];
1.2通过文件路径创建一个NSURL对象:+ (NSURL *)fileURLWithPath:(NSString *)path;//默认file协议。
NSURL *url = [NSURL URLWithString:@"file:///Users/apple/Desktop/3.txt"];
2把字符串写入NSURL对象:- (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError **)error;if([str writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:nil]){
NSLog(@"写入成功");}
3从URL路径中读取字符串:+ (nullable instancetype)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error;
NSString *str = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
1.拼接一字符串到某字符串最后面:- (void)appendString:(NSString *)aString;
[str appendString:@"abc"];
2.拼接一段格式化字符串到某字符串最后面:- (void)appendFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);
NSMutableString *str = [NSMutableStringstring];
[str appendFormat:@"score=%d",100];
3.删除range范围内的字符串:- (void)deleteCharactersInRange:(NSRange)range;
NSMutableString *str = [NSMutableString string];
[str appendString:@"lkjhgiuhg"];
[str deleteCharactersInRange:NSMakeRange(2, 4)];
NSLog(@"%@",str);//打印结果是:lkuhg
4.插入一个字符串:- (void)insertString:(NSString *)aString atIndex:(NSUInteger)loc;
NSMutableString *str = [NSMutableString string];
[str appendString:@"lkjhgiuhg"];
[str insertString:@"//"atIndex:3];
NSLog(@"%@",str);//打印结果是:lkj//hgiuhg
5.使用字符串替换range范围内的字符串: - (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)aString;
NSMutableString *str = [NSMutableString string];
[str appendString:@"lkjhgiuhg"];
[str replaceCharactersInRange:NSMakeRange(2, 3)withString:@"it"];
NSLog(@"%@",str);//打印结果是:lkitiuhg
NSMutableString *str = @"abc";
[strappendString:@"nah"];
运行时会报错。2.NSMutableString的string属性:覆盖原来字符串的内容
NSMutableString *str = [NSMutableString stringWithFormat:@"abc"];
str.string = @""
[str appendString:@"d"];
NSLog(@"%@",str);打印结果是:d
3.NSMutableString使用情况:需要做特殊处理(截取、拼接、替换)的时候。