int main(int argc, const char * argv[])
{
@autoreleasepool {
//NSString常用方法
//1. 创建字符串对象
//(1). 直接赋值
NSString *str1 = @"直接给值的字符串";
//(2). 初始化方法
NSString *str2 = [[[NSString alloc]initWithString:str1] autorelease];
//现有的字符串来创建
//(3). 便利构造器
NSString *str3 = [NSString stringWithString:str1];
NSString *str5 = @"非常";
NSString *str4 = [[NSString alloc]initWithFormat:@"今夜阳光明媚%@", str5];
NSLog(@"%@", str4);
NSString *str6 = [NSString stringWithFormat:@"今夜阳光%@明媚", str5];
NSLog(@"%@", str6);
//2. 获取字符串中某个位置上的字符
NSLog(@"%lu", sizeof(unichar));
unichar c = [str6 characterAtIndex:3];
NSLog(@"%C", c);
//3. 获取字符串的长度
NSLog(@"%lu", [str1 length]);
NSLog(@"%lu", [str2 length]);
NSLog(@"%lu", [str3 length]);
NSLog(@"%lu", [str4 length]);
NSLog(@"%lu", [str5 length]);
NSLog(@"%lu", [str6 length]);
//4. 获取字符串中得子串
NSString *sentence = @"梁立德和刘若曈是好基友";
NSString *liang = [sentence substringToIndex:3];
NSLog(@"%@", liang);
NSString *friend= [sentence substringFromIndex:8];
NSLog(@"%@", friend);
// NSRange range = {4, 3};//{区域, 长度}
NSString *liu = [sentence substringWithRange:NSMakeRange(4, 3)];
NSLog(@"%@", liu);
//5. 判断字符串是否以某个前缀开头
//例如:以下姓名字符串是否都姓"刘"开头
NSString *name1 = @"刘天娇";
// NSString *name2 = @"刘传瑞";
// NSString *name3 = @"王阳";
// NSString *name4 = @"刘旭";
// NSString *name5 = @"小黄人";
if ([name1 hasPrefix:@"刘"]) {
NSLog(@"%@姓刘", name1);
} else {
NSLog(@"%@不姓刘", name1);
}
//6. 判断字符串是否以某个后缀结束
//例如: 通过文件名判断是否是图片文件
//.jpg, .jpeg, .bmp, .gif, .png
NSString *fileName = @"yanZhaoMen.avi";
if ([fileName hasSuffix:@".jpg"] || [fileName hasSuffix:@".jpeg]"] || [fileName hasSuffix:@".bmp"] || [fileName hasSuffix:@".gif"] || [fileName hasSuffix:@".png"]) {
NSLog(@"文件%@是图像文件", fileName);
}
//7. 判断字符串中是否包含另一个字符串, 在什么位置
NSString *text = @"NSString primitive (funnel) methods. A minimal subclass of NSString just needs to implement these, although we also recommend getCharacters:range:. See below for the other methods.";
NSString *searchString = @"字符串";
NSRange range2 = [text rangeOfString:searchString];
NSLog(@"loc:%lu len:%ld", range2.location, range2.length);
NSLog(@"%lu", NSNotFound); //找不到 1. 则就是这个数, 枚举 2. length = 0
//不可变的类型
//8. 字符串拼接
NSString *s1 = @"Hard";
NSString *s2 = @"Work";
NSString *s = [s1 stringByAppendingString:s2];
NSLog(@"%@", s);
NSString *together = [NSString stringWithFormat:@"%@+%@", s1, s2];
NSLog(@"%@", together);
//整形常量 枚举
//9. 字符串替换(作业)
NSMutableString *st = [NSMutableString stringWithFormat:@"2B青年"];
[st replaceCharactersInRange: NSMakeRange(0,2) withString:@"文艺"];
NSLog(@"%@", st);
//10. 字符串比较
NSLog(@"A B %ld", [@"A" compare:@"B"]);
NSLog(@"B B %ld", [@"B" compare:@"B"]);
NSLog(@"B C %ld", [@"C" compare:@"B"]);
NSLog(@"BA BC %ld", [@"BA" compare:@"BC"]);
//11. 大小写之间的转换(作业)
NSString *str = @"kAbbbbFFFeCtt";
NSLog(@"%@",[str uppercaseString]);//将字符串中得所有小写字母转化为大写字符,不改变原来的字符串
NSLog(@"%@",[str lowercaseString]);//将字符串中得所有大写字母转化为小写字符,不改变原来的字符串
// NSLog(@"%@",[str capitalizeString]);//将字符串中出现的第一个字母转化为大写,其余字母小写
//12. 字符串和数值类型转换
//(1). 数字(基本数据类型)转字符串对象
NSString *num1 = [NSString stringWithFormat:@"%ld + %d = %d", 365L, 120, 365 + 120];
NSLog(@"%@", num1);
//(2). 字符串转数字类型
NSString *num2 = @"1234"; //int
NSString *num3 = @"3.14159"; //float
NSString *num4 = @"8888888888.6666"; //double
int a2 = [num2 intValue];
float a3 = [num3 floatValue];
double a4 = [num4 doubleValue];
NSLog(@"%d, %f, %f", a2, a3, a4); //输出结果精确度
//13. 字符串与数组
//(1). 字符串转数组
NSString *string = @"beiJing, Welcome, To, Here";
NSLog(@"string:%@", string);
NSArray *array = [string componentsSeparatedByString:@","];
NSLog(@"array:%@", array);
NSString *value = [array objectAtIndex:0];
NSLog(@"value:%@", value); //取出第0个元素
//(2). 数组转字符串
NSString *string1 = [array componentsJoinedByString:@","];
NSLog(@"string1:%@", string1);
//14. 字符串转化
//将OC字符串转化为C的字符串
NSString *string2 = @"Hello, worle!";
const char *p = [string2 UTF8String];
/*可变的字符串类 NSMutableString*/
//1. 创建
NSMutableString *stringM1 = [NSMutableString stringWithFormat:@"薛继苗"];
//2. 字符串拼接
[stringM1 appendString:@"的身高"];
[stringM1 appendFormat:@"%d米", 2];
NSLog(@"%@", stringM1);
//用不可变字符串的拼接方法
// stringM1 = [stringM1 stringByAppendingString:@"?"];
NSLog(@"%@", stringM1);
//3. 插入字符
[stringM1 insertString:@"今年" atIndex:0];
NSLog(@"%@", stringM1);
//4. 删除字符
[stringM1 deleteCharactersInRange:NSMakeRange(5, 5)];
NSLog(@"%@", stringM1);
//5. 查找并删除
NSRange substr = [stringM1 rangeOfString:@"年"];
if (substr.location != NSNotFound) {
[stringM1 deleteCharactersInRange:substr];
}
//6. 替换字符串
NSMutableString *st1 = [NSMutableString stringWithFormat:@"2B青年"];
[st1 replaceCharactersInRange: NSMakeRange(0,2) withString:@"文艺"];
//从第0个字符串处除掉2个字符, 并换成"文艺"
NSLog(@"%@", st1);
}
return 0;
}