//
// main.m
// OCdemo-04-00
//
// Created by lanou3g on 15/10/12.
// Copyright (c) 2015年 Object. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
/*
// NSString 不可改变的字符串
// 对象创建完成以后,内容和长度不能更改
// 1.创建字符串
//使用初始化方法
NSString *string1 = [[NSString alloc] initWithFormat:@"hello kity1"];
//使用类方法
NSString *string2 = [NSString stringWithFormat:@"hello kity2"];
//使用字面量方法
NSString *string3 = @"hello kity3";
//2.获取字符串长度
NSUInteger str1length = [string1 length];
NSLog(@"length = %lu",str1length);
//3.获取指定位置的字符(注意不能越界)
NSUInteger str2length = [string2 length];
char c1 = [string2 characterAtIndex:10];
char c2 = [string2 characterAtIndex:str2length - 1];
NSLog(@"%c",c1);
NSLog(@"%c",c2);
//4.判断字符串的前缀、后缀
BOOL isHasPrefix = [string3 hasPrefix:@"hello"];
NSLog(@"%d",isHasPrefix);
NSString *string4 = @"dahuang.png";
BOOL isHasSuffix = [string4 hasSuffix:@".png"];
NSLog(@"%d",isHasSuffix);
//5.查找某个字符串在另一个字符串的范围
// location : 开始位置
// length : 长度
NSString *string5 = @"北京蓝鸥科技有限公司";
NSString *string6 = @"蓝鸥科技";
NSRange range1 = [string5 rangeOfString:string6];
NSLog(@"location = %lu,length = %lu",range1.location,range1.length);
//6.字符串截取
//(1)通过位置和长度截取字符串
NSRange range2 = NSMakeRange(6, 4);
NSString *subStr5 = [string5 substringWithRange:range2];
NSLog(@"sunStr5 = %@",subStr5);
//(2)指定位置向后截取字符串
NSString *str2Str5 = [string5 substringFromIndex:2];
NSLog(@"%@",str2Str5);
//(3)指定位置向前截取
NSString *str3Str5 = [string5 substringToIndex:6];
NSLog(@"%@",str3Str5);
//7.字符串拼接
NSString *string7 = @"北京蓝鸥科技";
NSString *string8 = @"有限公司";
string7 = [string7 stringByAppendingString:string8];
NSLog(@"%@",string7);
//8.字符串替换
NSString *string9 = @"黄星杰";
string9 = [string9 stringByReplacingOccurrencesOfString:string9 withString:@"大黄"];
NSLog(@"string9 = %@",string9);
//9.字符串比较
NSString *string10 = @"huangxingjie";
NSString *string11 = @"liugaojian";
NSInteger result = [string10 compare:string11];
NSLog(@"%ld",result);
//判断某个字符串是否和另一个字符串相等
NSLog(@"%d",[string10 isEqualToString:@"huangxingjie"]);
//10.字符串和数值的类型转换
NSString *string12 = @"123";
NSInteger value1 = [string12 integerValue];
NSLog(@"%ld",value1);
//11.字符串大小写转换
NSString *string13 = @"ALu";
//把字符串转成小写
NSLog(@"%@",[string13 lowercaseString]);
//把字符串转成大写
NSLog(@"%@",[string13 uppercaseString]);
//字符串首字母大写
NSString *strName = @"lirui";
NSLog(@"%@",[strName capitalizedString]);
//-----------------------------------------
*/
// NSMutableString 可变的字符串
// 是NSString 的子类,该类创建的字符串是一个动态可变的
//1.创建字符串
NSMutableString *mstr1 = [NSMutableString stringWithFormat:@"xujin"];
//2.拼接字符串
[mstr1 appendString:@"周一见"];
NSLog(@"%@",mstr1);//xujin周一见
//3.插入字符
[mstr1 insertString:@"," atIndex:5];
NSLog(@"%@",mstr1); //xujin,周一见
//4.删除字符串
NSRange mrange1 = NSMakeRange(0, 6);
[mstr1 deleteCharactersInRange:mrange1];
NSLog(@"%@",mstr1); //周一见
//不可变的字符串的修改方法有返回值(重新指向新的字符串地址)
//可变的字符串的修改方法没有返回值,修改原字符串
//----------------------------------------------------------------------
//练习:给定一个图片文件名,判断字符串中是否以“png”结尾,如果是就替换成“jpg”,如果不是,就拼接”.jpg”
NSString *pic = @"tupian.png";
BOOL isHasSuffix = [pic hasSuffix:@".png"];
//NSLog(@"%d",isHasSuffix);
if (isHasSuffix == 0) {
pic = [pic stringByAppendingString:@".jpg"];
}else{
pic = [pic stringByReplacingCharactersInRange:NSMakeRange([pic length] - 3, 3) withString:@"jpg"];
}
NSLog(@"%@",pic);
}
return 0;
}