标签:
#pragma mark - 字符串创建
//常量字符串,存储在常量区(@"dsds"相同,则str,str6是同一个,地址相同)
NSString *str = @"dsds";
NSString *str6 = @"dsds";
//使用类方法创建字符串
NSString * string = [NSString string];
string = @"小白";//字符串赋值
//创建一个字符串,并且将内容设置为string
NSString * string1 = [NSString stringWithString:string];
//格式化创建字符串(按照格式输出)
NSString * string2 = [NSString stringWithFormat:@"%@",string1];
NSString * string0 = [NSString stringWithFormat:@"hello %d %c",5,‘A‘];
//实例方法初始化(比下面两种常用)
NSString *string3 = [[NSString alloc]init];
//实例方法指定字符串
NSString *string4 = [[NSString alloc]initWithString:string3];
//实例方法创建字符串
NSString *string5 = [[NSString alloc]initWithFormat:@"%@",string4 ];
#pragma mark - 字符串获取
NSString *string6 = [NSString stringWithFormat:@"helloiphone"];
//获取字符串长度(有效字符长度,不包括‘\0‘)
NSInteger len = [string6 length];
//字符串截取
//1.从哪个索引开始截取到字符串末尾(包含索引位置)
NSString *str0 = [string6 substringFromIndex:4];
//2.从字符串开头截取到哪个索引(不包含索引位置)
NSString *str1 = [string6 substringToIndex:8];
//3.截取一定范围(从下标3开始截取7个字符)
NSRange range = {3,7};
NSString *str2 = [string6 substringWithRange:range];
/*
typedef struct _NSRange {
NSUInteger location; //起始位置
NSUInteger length; //表示长度
} NSRange;
*/
//获取某字符串所在区间
NSString *string9 = @"ll";
NSRange rang1 = [string6 rangeOfString:string9];
NSRange rang2 = [string6 rangeOfString:@"pho"];
NSLog(@"************所在范围%@",NSStringFromRange(rang1));
if (rang2.location == NSNotFound) {
NSLog(@"没有这个子串");
}else{
NSLog(@"--------所在范围%@",NSStringFromRange(rang2));
}
if (rang1.length == 0) {
NSLog(@"没有这个子串");
}
//字符串比较
NSString *string7 = @"ahanweilong";
NSString *string8 = @"shanweilong";
//比较地址
if (string7 == string8) {
}
//比较内容
if ([string7 isEqualToString:string8]) {
NSLog(@"一样的~~");
}else{
NSLog(@"不一样~~");
}
if ([string7 compare:string8] == NSOrderedSame) {
NSLog(@"相等");
}
else if ([string7 compare:string8] == NSOrderedAscending)
{
NSLog(@"升序");
}
else if ([string7 compare:string8] == NSOrderedDescending)
{
NSLog(@"降序");
}
//NSCaseInsensitiveSearch 忽略大小写(@“123”,@“0123”字符串比较)
//NSNumericSearch 用于数值类型的比较(@“123”,@“0123”转化成数值比较)
NSComparisonResult result = [string7 compare:string8 options:NSCaseInsensitiveSearch];
if (result == NSOrderedSame) {
NSLog(@"相等");
}else if (result == NSOrderedAscending){
NSLog(@"升序");
}else if (result == NSOrderedDescending){
NSLog(@"降序");
}
}
NSString *string7 = @"ahanweilong";
NSString *string8 = @"shanweilong";
//检测字符是否以指定内容开头
if ([string7 hasPrefix:@"shan"]) {
NSLog(@"是的");
}
//检测字符串是否已指定内容结尾
if ([string7 hasSuffix:@"long"]) {
NSLog(@"是的");
}
//字符串拼接
NSString *string10 = [string7 stringByAppendingString:string8];
NSString *string11 = [NSString stringWithFormat:@"%@%@",string7,string8];
NSString *string12 = [string7 stringByAppendingFormat:@"-%@",string11];
//将数字字符串转换为数字(最常用)
NSString *string13 = @"123";
NSInteger integer = [string13 integerValue];
//字符串大写
NSString *string14 = @"fdjfep";
NSString *str14 = [string14 uppercaseString];
//字符串小写
NSString *string15 = @"GKHgIP";
NSLog(@"%@",[string15 lowercaseString]);
//首字母大写
NSLog(@"%@",[string14 capitalizedString]);
//替换
NSString *string16 = @"文艺青年";
string16 = [string16 stringByReplacingOccurrencesOfString:@"文艺" withString:@"2B"];
//删除原字符串中的所有指定字符
NSString * string18 = [string16 stringByReplacingOccurrencesOfString:@"文" withString:@""];
//从下标0开始2个
NSString *string17 = [string16 stringByReplacingCharactersInRange:NSMakeRange(0, 2) withString:@"二傻子"];
NSRange rang = [string16 rangeOfString:@"文艺"];
string17 = [string16 stringByReplacingCharactersInRange:rang withString:@"哈哈哈"];
//去掉两端空格
string18 = [string7 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
//C语言字符串转化成OC字符串
NSString *str20 = [NSString stringWithCString:"hehe" encoding:NSUTF8StringEncoding];
//OC语言字符串转化成C字符串
const char *cStr = [string8 cStringUsingEncoding:NSUTF8StringEncoding];
#pragma mark - 可变字符串
//一个字符串需要经常改变,就用NSMutableString
//Capacity表示容量,可以容纳多少字符,如果字符超过存储空间就扩展(尽量开辟时就够用,也不要太大)
NSMutableString *mutableString = [NSMutableString stringWithCapacity:6];
//类方法创建可变字符串
NSMutableString *mutableString1 = [NSMutableString string];
//在指定位置插入一个字符串对象(在第一个字符串后插入)
NSString *string1 = @"长得白";
NSMutableString * mutableString2 = [NSMutableString stringWithFormat:@"闪龙"];
[mutableString2 insertString:string17 atIndex:1];
NSLog(@"%@",mutableString2);
//删除字符串中某范围内容(从第0个删除3个)
[mutableString2 deleteCharactersInRange:NSMakeRange(0, 3)];
NSRange rang3 = {2,3};
[mutableString2 deleteCharactersInRange:rang3];
NSLog(@"%@",mutableString2);
//拼接可变字符串
NSString *string2 = @"拼了";
[mutableString2 appendString:string2];
[mutableString2 appendFormat:@"老夫和你%@",string2];
NSLog(@"%@",mutableString2);
//替换
[mutableString1 deleteCharactersInRange:rang3];
NSRange rrr = [mutableString1 rangeOfString:@"df"];
[mutableString2 replaceCharactersInRange:rrr withString:@"www"];
[mutableString2 replaceOccurrencesOfString:@"iiiiihj" withString:@"小白" options:NSLiteralSearch range:rrr];
//可变字符串赋值
NSMutableString *mutableString3 = [NSMutableString stringWithCapacity:0];
//NSMutableString *mutableString3 = [NSMutableString string];
[mutableString3 setString:@"树上蹲个??"];
// 不能用字面量方式赋值,字面量定义的是不可变的
// NSMutableString *mutableString4 = @"fdsf";
NSLog(@"%@",mutableString3);
标签:
原文地址:http://www.cnblogs.com/naruto-2/p/5116605.html