1 NSString
.1创建 alloc init stringWith
.2获取长度.length
.3获取字符 characterAtIndex
.4前缀和后缀的判断 hasPrefix hasSuffix
.5某个字符串在另一个字符串的范围(range)(NSMakeRange) rangeOfString
.6截取(sub) substringFromIndex WithRange To
.7拼接 stringByAppendingString
.8替换 stringByReplacingOccurrencesOfString
.9比较 compare
.10字符转和数值的类型转换 integerValue
.11大小写转换首字母大写capitalizedString
全大写 uppercaseString
全小写 lowercaseString
练习:
http://www.blog.csdn.net.png
// 判断上述字符串中是否以“png”结尾,如果是就替换成“jpg”,如果不是,就拼接”.jpg”。
2 NSMutableString
.1初始化(2)
.2拼接 appendString
.3插入 insertString
.4删除 deleteCharactersInRange
.5替换 replaceCharactersInRange
.6重置 setString
练习:用NSMutableString完成下述功能
http://www.blog.csdn.net.png
判断上述字符串中是否以“png”结尾,如果是就替换成“jpg”,如果不是,就拼接”.jpg”。
3 数值对象NSNumber(:NSValue)(一般用来操作基本数据类型)
.1创建(initWithInt numberWith @)(把数输入到数值对象中)
.2把数值对象转为基本数据类型
4 数值对象NSValue(一般用来存放数值结构体)
.1创建对象(把结构体(数值结构体)输入到对象中)
NSValue *value1 = [NSValue valueWithRange:NSMakeRange(0, 3)];
.2把数值指针转为数值对象(变相的创建指针)valueWithPointer
.3把数值对象转为结构体 rangeValue pointValue
(NSPoint NSRect NSSize NSRange)
.4把数值对象转为指针 pointerValue(point点 pointer指针)
==================以下为代码实现及讲解部分======================================
#import <Foundation/Foundation.h>
int main(int argc,const char * argv[]) {
@autoreleasepool {
/**
1.NSString
*/
//1.1定义
NSString *string1 = [[NSStringalloc]initWithFormat:@"iOS"];
NSString *string2 = [NSStringstringWithFormat:@"iOS"];
NSString *string3 =@"iOS"; //字面量方法
//1.2长度
NSLog(@"%ld", string1.length);
//1.3字符
unichar a = [string2characterAtIndex:2];
NSLog(@"%c", a);//unichar在OC中表示字符
//1.4前后缀
NSString *string4 =@"www.cctv.com";
BOOL hasPrefix = [string4hasPrefix:@"www"];
NSLog(@"%d", hasPrefix);
BOOL hasSuffix = [string4hasSuffix:@".com"];
NSLog(@"%d", hasSuffix);
//1.5范围
NSString *string5 =@"my first blog";
NSString *string6 =@"my first";
NSRange range1 = [string5rangeOfString:string6];
NSLog(@"location = %ld, length = %ld", range1.location, range1.length);
// NSMakeRange(<#NSUInteger loc#>, <#NSUInteger len#>)
//1.6截取
//分别截取 "my first blog"中三个单词
NSString *str1 = [string5substringToIndex:2];
NSLog(@"%@", str1);
NSString *str2 = [string5substringWithRange:NSMakeRange(3, 5)];
NSLog(@"%@", str2);
NSString *str3 = [string5substringFromIndex:9];
NSLog(@"%@", str3);
//1.7拼接
//拼接my first到my first blog后边
NSString *str4 = [string5stringByAppendingString:string6];
NSLog(@"%@", str4);
//1.8替换
NSString *strReplac = [string5stringByReplacingOccurrencesOfString:@"my"withString:@"your"];
NSLog(@"%@", strReplac);
//1.9比较
NSString *string7 =@"zhangsan";
NSString *string8 =@"lisi";
int compare = [string7compare:string8];
NSLog(@"%d", compare);
//1.10转成数值
NSString *string9 =@"123n45";
NSInteger integerValue = [string9integerValue];
NSLog(@"%ld", integerValue);
//1.11大小写转换
NSString *string10 =@"my blog";
string10 = [string10 capitalizedString];
NSLog(@"%@", string10);
string10 = [string10 uppercaseString];
NSLog(@"%@", string10);
string10 = [string10 lowercaseString];
NSLog(@"%@", string10);
//练习:http://www.blog.csdn.net.png
//判断上述字符串中是否以“png”结尾,如果是就替换成“jpg”,如果不是,就拼接”.jpg”。
NSString *stringText1 =@"http://www.blog.csdn.net.png";
if ([stringText1hasSuffix:@"png"]) {
stringText1 = [stringText1 stringByReplacingCharactersInRange:NSMakeRange(stringText1.length - 3, 3) withString:@"jpg"];
}
else stringText1 = [stringText1stringByAppendingString:@"jpg"];//注意看函数有无返回值
NSLog(@"%@", stringText1);
/**
* 2.NSmutableString
*/
//2.1定义
NSMutableString *mutableString1 = [[NSMutableStringalloc] initWithFormat:@"blog"];
NSMutableString *mutableString2 = [NSMutableStringstringWithFormat:@"blog"];
//没有第三种无法在self更改
//2.2拼接
[mutableString1 appendString:mutableString2];
NSLog(@"%@", mutableString1);
//2.3插入
[mutableString1 insertString:@" of my "atIndex:4];
NSLog(@"%@", mutableString1);
//2.4删除
[mutableString1 deleteCharactersInRange:NSMakeRange(4, 11)];
NSLog(@"%@", mutableString1);
//2.5替换
[mutableString1 replaceCharactersInRange:NSMakeRange(0, 4)withString:@"my first blog"];
NSLog(@"%@", mutableString1);
//2.6重置
[mutableString1 setString:@"my blog"];
NSLog(@"%@", mutableString1);
//练习:用NSMutableString完成下述功能:
//http://www.blog.csdn.net.png
//判断上述字符串中是否以“png”结尾,如果是就替换成“jpg”,如果不是,就拼接”.jpg”。
NSMutableString *mStringText1 = [NSMutableStringstringWithFormat:@"http://www.blog.csdn.net.png"];//stringWithString是浅拷贝指针一起复制 不开辟空间
if ([mStringText1hasSuffix:@"png"]) {
[mStringText1 replaceCharactersInRange:NSMakeRange(mStringText1.length - 3, 3) withString:@"jpg"];
}
else [mStringText1appendFormat:@".jpg"];
NSLog(@"%@", mStringText1); //注意多或少花括号容易引起不提示方法的问题
/**
* 3.NSNumber
*/
//3.1创建
NSNumber *number1 = [[NSNumberalloc]initWithInt:5];
NSNumber *number2 = [NSNumbernumberWithDouble:4.5];
NSNumber *number3 = @100;
NSLog(@"%@, %@, %@", number1, number2, number3);
//3.2数值对象转为基本数据类型
int intA = [number1intValue];
/**
* 4.NSValue
*/
//4.1创建
NSRange range =NSMakeRange(0, 3);
NSValue *value1 = [NSValuevalueWithRange:range];
//4.2数值指针传进NSValue对象
int b = 10;
int *p = &b;
NSValue *value2 = [NSValuevalueWithPointer:p];
//4.3把数值对象转为结构体
NSRange range2 = [value1rangeValue];
//4.4把数值对象转为指针
int *q = [value2pointerValue];
}
return 0;
}