//
// main.m
// NSString基本概念
// Foundation框架,苹果有80多个框架,Foundation有125个头文件。
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
//如何创建字符串对象
/*
通过不同的方式创建字符串,字符串对象储存的位置也不一样
>如果是通过字符串常量创建,那么字符串对象存储在常量区中
>如果是通过alloc initWithFormat/stringWithFormat创建,那么字符串对象存储在堆区中
而且需要注意:
>>alloc,不同的平台存储的方式也不一样,如果是Mac平台系统会自动对字符串对象进行优化,但是如果是iOS平台就是两个对象。
>>alloc,不同的编译器存储的方式也不一样,如果是Xcode6以下并且是在iOS平台,那么每次alloc都会创建一个新的对象,如果是在Xcode6以上那么alloc多次指向同一块存储空间。
*/
//1.通过字符串常量创建,在常量区
//注意:如果是通过字符串常量创建对象,并且字符串常量的内容一致,那么如果创建多个字符串对象,多个对象指向同一块存储空间
NSString *str1 = @"lnj";
NSString *str11 = @"lnj";
NSLog(@"str1 = %p, str11 = %p", str1 ,str11);//str1 = 0x100004238, str11 = 0x100004238
//2.通过alloc init创建,只要调用alloc就会在堆内存中开辟一块存储空间
NSString *str2 = [[NSString alloc]initWithFormat:@"lnj"];
NSString *str22 = [[NSString alloc]initWithFormat:@"lnj"];
NSLog(@"str2 = %p, str22 = %p", str2, str22);//str2 = 0x6a6e6c35, str22 = 0x6a6e6c35
//3.通过类工厂方法创建/ stringWithFormat
//内部其实就是封装了alloc init
NSString *str3 = [NSString stringWithFormat:@"zs"];
NSString *str33= [NSString stringWithFormat:@"zs"];
/*
注意:一般情况下,只要是通过alloc或者类工厂方法创建的对象,每次都会在堆内存中开辟一块新的存储空间
但是如果是通过alloc的initWithString方法除外,因为这个方法是通过copy返回一个字符串对象给我们
而copy又分为深拷贝和浅拷贝,如果是深拷贝会创建一个新的对象,如果是浅拷贝不会创建一个新的对象,而是直接返回被拷贝的对象的地址给我们
*/
NSString *str4 = [[NSString alloc]initWithString:@"ls"];
NSString *str44 = [[NSString alloc]initWithString:@"ls"];
NSLog(@"str4 = %p, str44 = %p", str4, str44);//str4 = 0x1000042b8, str44 = 0x1000042b8
return 0;
}
// main.m
// 字符串文件读写
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
/*
file: 文件路径,
encoding: 编码英文 iOS-5988-1 中文 GBK GBK2312 , 一般情况填写UTF-8
error: 如果读取错误, 会将错误信息保存到error中 ,如果读取正确, 就没有error = nil
注意: 以后在OC方法中但凡看到XXXofFile的方法, 传递的一定是全路径(绝对路径)
*/
NSString *path = @"/Users/mctc/Desktop/快捷键.txt";
NSError *error = nil;
// 从文件中读取字符串
NSString *str1 = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
if (error == nil) {
NSLog(@"str = %@", str1);
}else
{
NSLog(@"error = %@", [error localizedDescription]);//错误的真正原因
}
// 将字符串写入到文件中
NSString *str = @"iOS0601基础班";
// atomically 如果传入YES, 字符串写入文件的过程中如果没有写完, 那么不会生成文件
// 如果传入NO, 字符串写入文件的过程中如果没有写完, 会生成文件
NSString *path2 = @"/Users/mctc/Desktop/e.txt";
BOOL flag = [str writeToFile:path2 atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSLog(@"flag = %i", flag);
return 0;
}
//
// main.m
// 字符串读写2
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
//1.文件读取
//1.创建URL
//协议头 + 主机地址 + 文件路径
// NSString *path = @"file://192.168.199.199/Users/NJ-Lee/Desktop/lnj.txt"; file表示是本机的文件。ftp是文本传输协议。
// NSString *path = @"http://www.baidu.com";
//注意:如果加载的资源是本机上的资源,那么URL中的主机地址可以省略
//虽然主机地址可以省略,但是需要注意,文件路劲中最前面的/不能省略,文件路径最前面的/代表根路径
// NSString *path = @"file:///Users/NJ-Lee/Desktop/lnj.txt";
// NSURL *url = [NSURL URLWithString:path];
// NSString *path = @"file:///Users/NJ-Lee/Desktop/lnj.txt";
//注意:如果是通过NSURL的fileURLWithPath:方法创建URL,那么系统会自动给我们传入的字符串添加协议头(file://),所以字符串中不需要再写file://
// 注意:开发中一 般情况下,如果是访问本机的资源,创建URL的时候,建议使用fileURLWithPath方法创建
//因为url不支持中文,如果URL中包含中文,那么无法访问;但是如果是通过fileURLWithString方法创建URL,哪怕URL中包含中文也可以进行访问,系统内部会自动对URL中包含的中文进行处理
// NSURL *url = [NSURL fileURLWithPath:path];
NSString *path = @"file:///Users/NJ-Lee/Desktop/未命名文件夹/lnj.txt";
//如果URL中包含中文,又非不通过fileURLWithPath创建,也可以破
//如果想破就必须在创建URL之前先对字符串中的中文进行处理,进行百分号编码
NSLog(@"处理前:%@", path);
path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"处理后:%@", path);
NSURL *url = [NSURL URLWithString:path];
NSLog(@"url = %@", url);
//2.根据URL加载文件中的字符串
NSString *str = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSLog(@"str = %@", str);
//2.文件写入
NSString *str1 = @"lnj";
// NSString *path = @"file:///Users/NJ-Lee/Desktop/未命名文件夹/abc.txt";
// path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// NSURL *url = [NSURL URLWithString:path];
NSString *path1 = @"/Users/NJ-Lee/Desktop/未命名文件夹/abc.txt";
NSURL *url1 = [NSURL fileURLWithPath:path1];
[str1 writeToURL:url1 atomically:YES encoding:NSUTF8StringEncoding error:nil];
//注意点:如果多次往同一个文件中写入内容,那么后一次的会覆盖前一次的
NSString *str22 = @"xxoo";
[str22 writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:nil];
return 0;
}
//
// main.m
// 字符串比较
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
NSString *str1 = @"abc";
NSString *str2 = @"ABC";
// 比较两个字符串的"内容"是否相同
BOOL flag = [str1 isEqualToString:str2];
NSLog(@"flag = %i", flag);
// 下面这个方法, 是比较两个字符串的"地址"是否相同
flag = (str1 == str2);
NSLog(@"flag = %i", flag);
// 比较字符串的大小
// NSOrderedAscending 前面的小于后面的
// NSOrderedSame, 两个字符串相等
// NSOrderedDescending 前面的大于后面的
switch ([str1 compare:str2]) {
case NSOrderedAscending:
NSLog(@"str1小于str2");
break;
case NSOrderedSame:
NSLog(@"str1等于str2");
break;
case NSOrderedDescending:
NSLog(@"str1大于str2");
break;
default:
break;
}
// 忽略大小写进行比较
switch ([str1 caseInsensitiveCompare:str2]) {
case NSOrderedAscending:
NSLog(@"str1小于str2");
break;
case NSOrderedSame:
NSLog(@"str1等于str2");
break;
case NSOrderedDescending:
NSLog(@"str1大于str2");
break;
default:
break;
}
return 0;
}
//
// main.m
// 字符串搜索
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
NSString *str = @"http://www.520it.com/img/lnj.gif";
// 1.判断是否以什么开头
// 本质就是从字符串的第一个字符开始匹配, 只要不匹配就返回NO
if ([str hasPrefix:@"http://"]) {
NSLog(@"是一个URL");
}else
{
NSLog(@"不是一个URL");
}
// 2.判断是否以什么结尾
// 本质就是从字符串的最后一个字符开始匹配, 只要不匹配就返回NO
if ([str hasSuffix:@".gif"]) {
NSLog(@"动态图片");
}else{
NSLog(@"不是动态图片");
}
// 3.判断字符串中是否包含520it.com
NSString *str = @"abcd";
// 只要str中包含该字符串, 那么就会返回该字符串在str中的起始位置以及该字符串的长度
// location从0开始 , length从1开始
// 如果str中没有需要查找的字符串, 那么返回的range的length=0, location = NSNotFound
NSRange range = [str rangeOfString:@"lnj"];
// if (range.location == NSNotFound) {
if (range.length == 0){
NSLog(@"str中没有需要查找的字符串");
}else{
NSLog(@"str中有需要查找的字符串");
NSLog(@"location = %lu, length = %lu", range.location, range.length);
}
return 0;
}