标签:
main.m
//
// main.m
// Foundation
//
// Created by zhaogang on 16/5/6.
// Copyright ? 2016年 zhaogang. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
NSError *error = nil;
NSString *path = @"/Users/zzzigo/Desktop/test.txt";
NSString *str = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
NSLog(@"%@", str);
//同样url也可以用,同之前的写入
//[NSString alloc] initWithContentsOfURL:<#(nonnull NSURL *)#> encoding:<#(NSStringEncoding)#> error:<#(NSError * _Nullable __autoreleasing * _Nullable)#>
return 0;
}
void export()
{
NSString *str = @"jack";
NSString *path = @"/Users/zzzigo/Desktop/test.txt";
NSError *error = nil;
//写入文件
//[str writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
//也是写入一个文件
//NSURL *url = [NSURL URLWithString:@"file:///Users/zzzigo/Desktop/test.txt"];
NSURL *url = [NSURL fileURLWithPath:@"/Users/zzzigo/Desktop/test.txt"];
[str writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:&error];
if(error)
NSLog(@"write fail! error = %@ !" , [error localizedDescription]);//error
else
NSLog(@"write sucess");
}
void mutableStr()
{
//NSString是不可变字符串
//可变字符
NSMutableString *str = [[NSMutableString alloc] initWithCapacity:0];
[str appendFormat:@"123"];
NSString *str1 = @"123";
//追加一个字符串
NSString *str2 = [str1 stringByAppendingString:@"aaa"];
//找到一个字符串的范围
NSRange range3 = [str1 rangeOfString:@"3"];
//用字符串的范围删除这个字符串
[str deleteCharactersInRange:range3];
//插入字符串
[str insertString:@"aaa" atIndex:0];
//替换
[str replaceOccurrencesOfString:@"3" withString:@"" options:NSLiteralSearch range:NSMakeRange(0, str.length) ];
NSLog(@"%@",str);
}
void stringis()
{
NSString *str = @"qq=444381246&pwd=jiuge1";
//检测是否以qq=开头
[str hasPrefix:@"qq="];
//检测是否以jiuge1结尾
[str hasSuffix:@"jiuge1"];
}
void test()
{
//这两个是等价的
NSString *str = @"jack";
NSString *str1 = [[NSString alloc] initWithString:@"jack"];
//类似于c里面的sprintf
NSString *str2 = [[NSString alloc] initWithFormat:@"%d, %f",10 , 1.f];
// http://im.qq.com?qq=123&pwd=123
NSString *qqURL = @"http://im.qq.com?qq=444381246&pwd=jiuge1";
//取得qq在url里面的范围
NSRange qqRange = [qqURL rangeOfString:@"qq="];
//或者 qqRange.location == NSNotFound
if(qqRange.length)
NSLog(@"len = %zd, loc = %zd",qqRange.length, qqRange.location);
else
NSLog(@"nothing!");
NSRange pwdRange = [qqURL rangeOfString:@"&pwd="];
//取出账号的范围
NSRange range = NSMakeRange(qqRange.location + qqRange.length, pwdRange.location - (qqRange.length + qqRange.location));
//账号
//根据range切割字符串
NSString *qqId = [qqURL substringWithRange: range];
NSLog(@"qqId = %@", qqId);
//密码
//根据index切割字符串
NSString *qqPwd = [qqURL substringFromIndex:pwdRange.location + pwdRange.length];
NSLog(@"qqPwd = %@", qqPwd);
//1.将字符串变为大小写字母,
NSString *str3 = nil;
//str =[qqPwd lowercaseString];
str = [qqPwd uppercaseString];
//2.两个字符串比较
if([str isEqualToString:@"JIUGE1"])
NSLog(@"pwd is right!");
else
NSLog(@"pwd is wrong!");
}
void range()
{
//表示一个范围,比如@"12345"
NSRange range = {1, 2}; //表示字符串里的 2 3
NSRange range2 = {.location = 1, .length = 3};//表示字符串里的 2 3 4
NSRange range3 = NSMakeRange(1, 3); //表示字符串里的 2 3 4
}
void point()
{
//NSPoint CGPoint 表示平面里面的一个点
CGPoint pt = {1, 2};
CGPoint pt1 = {.x = 1, .y = 2};
CGPoint pt2 = NSMakePoint(1, 3);
}
void size()
{
//大小,有宽和高
//NSSize CGSize
CGSize size = NSMakeSize(10, 100);
//前两个参数表示坐标
CGRect rect = NSMakeRect(0, 0, 100, 100);
}
Object-c------Foundation中的结构体和字符串
标签:
原文地址:http://blog.csdn.net/qq_33351410/article/details/51333762