//1.分割
NSString *urlString = @"www.beiwoit.com";
/**
* 将参数作为分隔符,将字符串分割成若干部分
*/
NSArray *array = [urlString componentsSeparatedByString:@"."];
NSLog(@"%@",array);
//2.获取字符
char character = [urlString characterAtIndex:4];
NSLog(@"%c",character);
//3.截取 substring
//截取到下标(不包括下标字符)
NSLog(@"%@",[urlString substringToIndex:5]);
//自下标截取(包括下标字符)
NSLog(@"%@",[urlString substringFromIndex:5]);
//范围截取
/**
* typedef struct _NSRange {
NSUInteger location;//位置 -> 下标 -> 4
NSUInteger length;//长度 -> 长度 -> 7
} NSRange;
*/
//c创建结构体
// NSRange range1 = {4,7};
//oc创建结构体
NSRange range = NSMakeRange(4, 7);
NSLog(@"%@",[urlString substringWithRange:range]);
//4.拼接 appending
NSString *appString = @"拼接字符串:";
//拼接字符串
NSLog(@"%@",[appString stringByAppendingString:@"尾端添加字符串"]);
//拼接格式化字符串
NSLog(@"%@",[appString stringByAppendingFormat:@"尾端添加格式化字符串%d",100]);
//拼接扩展名 .pdf
NSLog(@"%@",[appString stringByAppendingPathExtension:@"dmg"]);
//拼接路径 /path
NSLog(@"%@",[appString stringByAppendingPathComponent:@"dmg"]);
//5.查找
NSString *findString = @"/Users/CORYIL/Library/Developer/CoreSimulator/Devices/88A633A0-6294-4853-80DE-8060C1580820/data/Containers/Data/Application/84CE535F-49D1-498E-89DB-2F07C50D6A24/Documents/";
NSRange findResult = [findString rangeOfString:@"Documents"];
//将结构体转化为字符串 NSStringFromXXX(结构体)
NSLog(@"%@",NSStringFromRange(findResult));
//6.替换 replacing
//根据range替换
NSString *newString = [findString stringByReplacingCharactersInRange:findResult withString:@"嘿嘿"];
NSLog(@"%@",newString);
//直接替换
NSLog(@"%@",[findString stringByReplacingOccurrencesOfString:@"Users" withString:@"用户们"]);
2016-01-18 20:11:35.733 02NSString字符串[4039:618330] (
www,
beiwoit,
com
)
2016-01-18 20:11:35.733 02NSString字符串[4039:618330] b
2016-01-18 20:11:35.733 02NSString字符串[4039:618330] www.b
2016-01-18 20:11:35.733 02NSString字符串[4039:618330] eiwoit.com
2016-01-18 20:11:35.733 02NSString字符串[4039:618330] beiwoit
2016-01-18 20:11:35.733 02NSString字符串[4039:618330] 拼接字符串:尾端添加字符串
2016-01-18 20:11:35.734 02NSString字符串[4039:618330] 拼接字符串:尾端添加格式化字符串100
2016-01-18 20:11:35.734 02NSString字符串[4039:618330] 拼接字符串:.dmg
2016-01-18 20:11:35.734 02NSString字符串[4039:618330] 拼接字符串:/dmg
2016-01-18 20:11:35.734 02NSString字符串[4039:618330] {161, 9}
2016-01-18 20:11:35.734 02NSString字符串[4039:618330] /Users/CORYIL/Library/Developer/CoreSimulator/Devices/88A633A0-6294-4853-80DE-8060C1580820/data/Containers/Data/Application/84CE535F-49D1-498E-89DB-2F07C50D6A24/嘿嘿/
2016-01-18 20:11:35.735 02NSString字符串[4039:618330] /用户们/CORYIL/Library/Developer/CoreSimulator/Devices/88A633A0-6294-4853-80DE-8060C1580820/data/Containers/Data/Application/84CE535F-49D1-498E-89DB-2F07C50D6A24/Documents/
Program ended with exit code: 0
Day.01.18 字符串的截取
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 首先要了解中文字符有多种编码及各种编码的特征。假设n为要截取的字节数。
- 一、PHP截取两个指定字符中间的字符 $a = "123@abc#456"; $b= (strpos($a,"@"...