我说常说v这个变量时,表示它是在栈里。
我们说v这个对象,表示是v这个变量指向的对象,但凡是指向的某个对象,都是在堆里。
字符串的一些常用方法
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
#if 0
NSString *str = @"hello world";
NSString *str2 = @"hello world_";
// str2 = str;
NSLog(@"str = %p",str);
NSLog(@"str2 = %p",str2);
NSString *s1 = @"hello world";
s1 = @"hello welcome";
#endif
NSString *str = @"hello world welcome to here";
//从下标0位打印到10止
NSString *subString = [str substringWithRange:NSMakeRange(0, 10)];
NSLog(@"subString = %@",subString);
//从下标第6位打印到结束
subString = [str substringFromIndex:6];
NSLog(@"subString = %@",subString);
//打印到第6位结束
subString = [str substringToIndex:6];
NSLog(@"subString = %@",subString);
#if 0
NSString *str1 = @"hello world";
NSString *str2 = @"hello world_";
if(str1 == str2){
NSLog(@"==");
}
#endif
#if 0
NSString *url = @"http://xxx.xxx.com";
//打印conrains包含的
if([url containsString:@"xxx"]){
NSLog(@"xxx");
}
//打印以这个结尾的
if([url hasSuffix:@"com"]){
NSLog(@"com");
}
//打印以这个开头的
if([url hasPrefix:@"http"]){
NSLog(@"http");
}
#endif
NSRange range = [str rangeOfString:@"welcomp"];
NSLog(@"%ld",range.location);
if(range.location == NSNotFound){
}
//拼接地址
NSString *basePath = @"base";
//不会自动生成 / 符号
NSString *finalPath = [basePath stringByAppendingString:@"/Documents"];
//自动生成 / 符号
[basePath stringByAppendingPathComponent:@"Documents"];
//整数转化为字符串
NSString *numStr = @"55";
numStr.integerValue;
NSInteger a = 55;
[NSString stringWithFormat:@"%ld",a];
//全部都转换为小写
NSString *name = @"ZhangSAN";
NSLog(@"name = %@",[name lowercaseString]);
//把数据转换成UTF-8
NSData *data = [@"hello world" dataUsingEncoding:NSUTF8StringEncoding];
//将字符串转化为数组
NSArray *arr = [str componentsSeparatedByString:@"world"];
NSLog(@"arr = %@",arr);
//打印的zhangsan这个名字前面有很多的空格,空格是占用空间的。
NSString *userName = @" zhangsan";
NSLog(@"userName = %@",userName);
//这个方法是把多余的空格都去掉。
NSString *finalUserName = [userName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSLog(@"finalUserName = %@",finalUserName);
//把特定的字符都改写成我们需要的字符,让某些内容不显示出来。比如说把world,改写成*号
NSString *secrStr = [str stringByReplacingOccurrencesOfString:@"world" withString:@"*"];
//把某个地址或者网址的文章,读取到文件里。
NSString *contents = [NSString stringWithContentsOfFile:@"--filePath--" encoding:NSUTF8StringEncoding error:nil];
//把某个地址或者网站的文章,写入到文件里。
[@"contetns" writeToFile:@"--target path" atomically:YES encoding:NSUTF8StringEncoding error:nil];
}
@end