字符串
//C语言字符串
//1.每个字符占用一个字节空间
//2.字符串以'\0'结束
//3.字符串用""包含
//4.打印字符串用%s
//5.字符串中的每个字符以ASCII码形式存储
//字符串对象
//1.用@" "包含表示是一个字符串对象
//2.字符串对象中的每个字符占用空间大小不确定
//3.字符以UTF-8编码形式存储,UTF-8是一个多字节编码,属于Unicode编码
//4.打印字符串对象用 %@
//OC中的字符串是一个字符串对象
//NSString NSMutableString
//NSString 创建不可变字符串对象
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *str= @"hello world中国";//常量字符串对象存储在常量区
NSLog(@"%@", str);
NSLog(@"%p", str);
//用给定格式构造一个字符串对象
NSString *str1 = [[NSString alloc] initWithFormat:@"%s%d%f","hellowrld",1000,3.14];
NSLog(@"str1 = %@", str1);
//用另外一个字符串对象构造一个新的字符串对象
NSString *str2 = [[NSString alloc] initWithString:str1];
NSLog(@"str2 = %@", str2);
//用C语言的字符串构造一个OC字符串对象
NSString *str3 = [[NSString alloc] initWithUTF8String:"qianfeng"];
NSLog(@"str3 = %@", str3);
NSString *str4 = [[NSString alloc] initWithCString:"1000phone" encoding:NSUTF8StringEncoding];
NSLog(@"str4 = %@", str4);
//利用类方法创建OC字符串对象
//构造一个空的字符串对象 @""
NSString *str5 = [NSString string];
NSLog(@"str5 = %@", str5);
//用给定的格式创建一个新的字符串对象
NSString *str6 = [NSString stringWithFormat:@"%d%s%@",200,"helloworld",@"qianfeng"];
NSLog(@"str6 = %@", str6);
//用给定的字符串对象创建一个新的字符串对象
NSString *str7 = [NSString stringWithString:str6];
NSLog(@"str7 = %@", str7);
//用C语言字符串创建OC字符串对象
NSString *str8 = [NSString stringWithUTF8String:"中国教育"];
NSLog(@"str8 = %@", str8);
//用C语言的字符串创建OC字符串对象
NSString *str9 = [NSString stringWithCString:"千锋教育" encoding:NSUTF8StringEncoding];
NSLog(@"str9 = %@", str9);
}
return 0;
}
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *str = [NSString stringWithCString:"helloworld中国教育" encoding:NSUTF8StringEncoding];
//获取字符串对象长度
NSUInteger len = [str length];
NSLog(@"len = %lu %@", len,str);
//获取下标位置的unichar字符
unichar ch = [str characterAtIndex:11];
NSLog(@"ch = %C", ch);//OC中的字符打印用%C
//提取子串
//从下标位置提取子串到字符串结束
NSString *str1 = [str substringFromIndex:5];
NSLog(@"str1 = %@",str1);
//从字符串开始提取子串到下标位置(不包含下标位置的字符)
NSString *str2 = [str substringToIndex:5];
NSLog(@"str2 = %@", str2);
//提取指定范围内的子串
NSRange range = {5,6};
NSString *str3 = [str substringWithRange:range];
NSLog(@"str3 = %@", str3);
NSString *str4 = [str substringWithRange:NSMakeRange(5, 5)];//NSMakeRange 创建一个range
NSLog(@"str4 = %@", str4);
//字符串比较
NSString *str5 = @"helhelloloworld";
NSString *str6 = @"hell";
NSComparisonResult ret = [str5 compare:str6];
if (ret == NSOrderedAscending) {
NSLog(@"str5 < str6");
}
else if (ret == NSOrderedSame)
{
NSLog(@"str5 == str6");
}
else if (ret == NSOrderedDescending)
{
NSLog(@"str5 > str6");
}
//以大小写不敏感方式比较字符串对象
NSComparisonResult ret2 = [str5 compare:str6 options:NSCaseInsensitiveSearch];
NSLog(@"ret2 = %ld", ret2);
NSComparisonResult ret3 = [str5 caseInsensitiveCompare:str6];
NSLog(@"ret3 = %ld", ret3);
//range: 值的调用对象的范围,range.length表示最多比较字符的个数
NSComparisonResult ret4 = [str5 compare:@"hello" options:NSLiteralSearch range:NSMakeRange(3, 5)];
NSLog(@"ret10 = %ld", ret4);
//判断两个字符串对象是否相等, 相等返回1,否则返回0
BOOL ret5 = [str5 isEqualToString:str6];
NSLog(@"ret4 = %d", ret5);
//判断前缀
// - (BOOL)hasPrefix:(NSString *)aString;
NSString *str7 = @"www.baidu.com";
BOOL ret6 = [str7 hasPrefix:@"www."];
NSLog(@"ret5 = %d", ret6);
//判断后缀
// - (BOOL)hasSuffix:(NSString *)aString;
NSString *str8 = @"baidu.com";
BOOL ret7 = [str8 hasSuffix:@"com"];
NSLog(@"ret6 = %d", ret7);
//判断是否包含子串
//- (BOOL)containsString:(NSString *)aString;
BOOL ret8 = [@"helloworld" containsString:@"hello"];
NSLog(@"ret8 = %d", ret8);
//查找子串
//查找不到子串返回long类型的最大值 (NSNotFound)
NSString *str9 = @"qianfengjiaoyu";
NSRange range1 = [str9 rangeOfString:@"hello"];
if(range1.location == NSNotFound)
{
NSLog(@"%lu 没有找到指定的字符串", NSNotFound);
}
else
{
NSLog(@"location = %ld , length = %ld", range1.location,range1.length);
}
//倒序查找子串
NSRange range2 = [@"qianhelloworldhellofeng" rangeOfString:@"hello" options:NSBackwardsSearch];
NSLog(@"location = %lu, length = %lu", range2.location, range2.length);
//字符串追加
NSString *str10 = [NSString stringWithFormat:@"%s","qianfeng"];
NSLog(@"%p", str10);
str10 =[str10 stringByAppendingString:@"helloworld"];//创建一个新的字符串对象
NSLog(@"%p", str10);
NSLog(@"str10 = %@", str10);
//格式化追加字符串
NSString *str11 = [str10 stringByAppendingFormat:@"%f%s%d",3.14,"qianfeng",1000];
NSLog(@"str11 = %@", str11);
//把字符串转换成数字
NSLog(@"%ld",[@"123" integerValue]);
NSLog(@"%.2f",[@"-3.1456" floatValue]);
//返回公共前缀子串
NSString *str12 = [@"www.baidu.com" commonPrefixWithString:@"www.hao123.com" options:NSLiteralSearch];
NSLog(@"str12 = %@", str12);
//小写转换大写
NSString *str13 = [@"hello world qian" uppercaseString];
NSLog(@"str13 = %@", str13);
//大写转换小写
NSString *str14 = [@"HEllo WORld" lowercaseString];
NSLog(@"str14 = %@", str14);
//把每个单词的首字母转换成大写
NSString *str15 = [@"hello qian feng world" capitalizedString];
NSLog(@"str15 = %@", str15);
//把OC字符串转换成C语言字符串
const char *str16 = [@"www.baidu.com" cStringUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%s",str16);
//字符串替换
NSString *str17 = [NSString stringWithUTF8String:"worldqianfengworld1000feng"];
//用新的字符换替换所有指定的字符串
str17 = [str17 stringByReplacingOccurrencesOfString:@"world" withString:@"www"];
NSLog(@"str17 = %@", str17);
//用新的字符串替换指定范围内的字符串
NSString *str18 = [str17 stringByReplacingCharactersInRange:NSMakeRange(7, 7) withString:@"sina.cn"];
NSLog(@"str18 = %@", str18);
//利用网址的内容创建字符串对象
//- (instancetype)initWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error;
//+ (instancetype)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error;
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
NSString *str19 = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSLog(@"str19 = %@", str19);
//利用文件内容创建字符串对象
//- (instancetype)initWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error;
//+ (instancetype)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error;
NSString *str20 = [NSString stringWithContentsOfFile:@"/Users/zhangxueming/Desktop/file.txt" encoding:NSUTF8StringEncoding error:nil];
NSLog(@"str20 = %@", str20);
}
return 0;
}
//NSMutableString
//可变字符串
//可变字符串对象可以被修改
//继承与不可变字符串
//不可变字符串的方法, 可变字符串都可以使用
int main(int argc, const char * argv[]) {
@autoreleasepool {
//在指定的位置增加字符串
NSMutableString *mulStr = [NSMutableString stringWithUTF8String:"helloworld"];
[mulStr insertString:@"qianfeng" atIndex:5];
NSLog(@"mulStr = %@", mulStr);
//删除指定范围内的字符
[mulStr deleteCharactersInRange:NSMakeRange(9, 4)];
NSLog(@"mulStr = %@", mulStr);
//追加字符串
[mulStr appendString:@"baidu.com"];
NSLog(@"mulStr = %@", mulStr);
//格式化追加字符串
NSMutableString *mulStr1 = [NSMutableString stringWithCString:"中国人" encoding:NSUTF8StringEncoding];
[mulStr1 appendFormat:@"%@",@"在大连"];
NSLog(@"mulStr1 = %@", mulStr1);
//修改字符串对象
[mulStr1 setString:@"helloworld"];
NSLog(@"mulStr1 = %@", mulStr1);
//构造可变字符串对象并设定容量大小,但是可变容量可以被修改
//+ (NSMutableString *)stringWithCapacity:(NSUInteger)capacity;
NSMutableString *mulStr2 = [[NSMutableString alloc]initWithCapacity:20];
[mulStr2 setString:@"千锋"];
NSLog(@"mulStr2 = %@", mulStr2);
//字符串替换
NSMutableString *mulStr3 = [NSMutableString stringWithCapacity:10];
[mulStr3 appendString:@"helloOnehelloTwohelloThree"];
//NSLog(@"%@", mulStr3);
//替换指定范围内的指定所有字符串
// [mulStr3 replaceOccurrencesOfString:@"hello" withString:@"welcome" options:NSLiteralSearch range:NSMakeRange(0, 16)];
[mulStr3 replaceOccurrencesOfString:@"hello" withString:@"welcome" options:NSLiteralSearch range:NSMakeRange(0, [mulStr3 length] )];
NSLog(@"mulStr3 = %@", mulStr3);
//替换(删除)指定范围内的所有字符
[mulStr3 replaceCharactersInRange:NSMakeRange(7, 10) withString:@""];
NSLog(@"mulStr3 = %@", mulStr3);
}
return 0;
}
posted on 2014-02-08 20:42 Sinner_Yun 阅读(312) 评论(0) 编辑 收藏 举报