ios字符串使用详解(二)
2016-07-12 本文已影响129人
伊蕊飘零
// 创建字符串
- (void)CreatString
{
// <1> 创建字符创
NSString *strS = @"This is a String!";
// <2> 创建空字符串,给予赋值
NSString *strK = [[NSString alloc] init];
strK = @"This is a String!";
NSLog(@"strS:%@, strK%@",strS,strK);
// <3> 使用变量初始化字符串
NSString *name = @"aofe";
NSString *strN = [NSString stringWithFormat:@"My name is %@",name];
NSLog(@"strN:%@",strN);
}
// 判断是否包含某字符串
- (void)IsContainsString
{
NSString *str1 = @"NSStringInformation.txt";
// <1> 检查字符串是否以另一个字符串开头
[str1 hasPrefix:@"NSString"] == 1? NSLog(@"YES") :NSLog(@"NO");
// <2> 检查字符串是否以另一个字符串结尾
[str1 hasSuffix:@".txt"] == 1? NSLog(@"YES") :NSLog(@"NO");
// <3> 检查字符串是否包含其他字符串
[str1 rangeOfString:@"Information"].length > 0 ? NSLog(@"YES") :NSLog(@"NO");
}
// 读写字符串
-(void)WriteAndReadString
{
// 文件路径
NSString *path = @"wenJianQuanLuJing";
// <1> 从文件读取字符串
NSString *strW = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];
// <2> 将字符串写入到文件
[strW writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:NULL];
}
// 比较两个字符串
-(void)ComparerTwoString
{
NSString *str1 = @"This is String1";
NSString *str2 = @"THIS is String2";
// 比较两个字符串是否相等
BOOL result1 = [str1 isEqualToString:str2];
// 比较两个字符串(comparer方法返回三种值:NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDescending)
NSComparisonResult result2 = [str1 compare:str2];
// 不考虑大小比较字符串
NSComparisonResult result3 = [str1 caseInsensitiveCompare:str2];
NSLog(@"result1:%d,result2:%ld,result3:%ld",result1,(long)result2,(long)result3);
}
// 改变字符串的大小写
-(void)ChangeStringCase
{
NSString *str1 = @"this is string1";
NSString *str2 = @"THIS IS STRING2";
// 全部大写
[str1 uppercaseString];
// 全部小写
[str2 lowercaseString];
// 首字母大写
[str1 capitalizedString];
}
// 在字符串中搜索子串
-(void)SearchString
{
NSString *str1 = @"This is String1";
NSString *str2 = @"is";
NSRange range = [str1 rangeOfString:str2];
NSLog(@"location:%lu,length:%lu",(unsigned long)range.location,(unsigned long)
range.length);
}
// 替换字符串
-(void)ReplaceString
{
NSString *strL = @"hello china";
NSString *strN = [strL stringByReplacingOccurrencesOfString:@"china" withString:@"beijing"];
NSLog(@"strL:%@,strN:%@",strL,strN);
}
// 分隔字符串成数组
-(void)ComponentsString
{
NSString *str = @"a b c d e";
// 以空格分隔字符串成数组
NSArray *arr = [str componentsSeparatedByString:@" "];
NSLog(@"arr:%@,arr.count:%lu",arr,(unsigned long)arr.count);
}
// 数组拼接成字符串
-(void)ArrayToString
{
NSArray *array = [NSArray arrayWithObjects:@"this",@"is",@"String", nil];
// 用空格隔开数组中的元素
NSString *str = [array componentsJoinedByString:@" "];
NSLog(@"str:%@",str);
}
// 从字符串中抽取出新的字符串
-(void)DrawNewString
{
NSString *str = @"This is String";
// 从字符串的开头一直截取到指定位置,但不包括该位置的字符.
NSString *str1 = [str substringToIndex:5];
// 从指定位置开始(包括自定位置的字符串)一直到最后
NSString *str2 = [str substringFromIndex:5];
// 按照所给定的位置和长度,任意的从字符串中截取新的字符串
NSString *str3 = [str substringWithRange:NSMakeRange(4, 5)];
NSLog(@"str1:%@,str2:%@,str3:%@",str1,str2,str3);
}
// 可变字符串的操作
- (void)NSMutableStringOperation
{
// 给字符串分配容量
NSMutableString *strM = [NSMutableString stringWithCapacity:100];
NSLog(@"strM:%@",strM);
// 在已有的字符串后面添加字符串
NSMutableString *strM1 = [[NSMutableString alloc] initWithString:@"This is a"];
NSLog(@"strM1:%@",strM1);
[strM1 appendString:@"NSMutableString"];
NSLog(@"strM1:%@",strM1);
// 在已有字符串中按照所给出的范围和长度删除字符
[strM1 deleteCharactersInRange:NSMakeRange(0, 5)];
NSLog(@"strM1:%@",strM1);
// 在字符串指定位置插入字符串
[strM1 insertString:@"Hello" atIndex:0];
NSLog(@"strM1:%@",strM1);
// 将已有字符串替换成其他字符串
[strM1 setString:@"Hello World"];
NSLog(@"strM1:%@",strM1);
// 按照所给出的范围,用新字符串替换原来的字符串
[strM1 replaceCharactersInRange:NSMakeRange(0, 5) withString:@"Hi"];
NSLog(@"strM1:%@",strM1);
}