iOS Developer

OC中字符串的使用总结

2016-11-15  本文已影响489人  Mark_Guan

componentsJoinedByStringcomponentsSeparatedByString:

//将string字符串转换为array数组
 NSArray  *array = [Str componentsSeparatedByString:@","];

==反向方法
//将array数组转换为string字符串
 NSString *tempString = [mutableArray componentsJoinedByString:@","];--分隔符

stringByTrimmingCharactersInSet:过滤字符串中的特殊符号

//去除两端的空格
 [TextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

//去除回车
[TextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet ]];

//去除@符号和双引号
[TextField.text stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"@\""]

NSString 与 NSData 的转化

//NSData --> NSString
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

NSString *str2 = @"1234";
NSData *aData = [str2 dataUsingEncoding: NSUTF8StringEncoding];

字符串与SEL类型的转换

    // 将一个字符串方法转换成为SEL对象
    SEL s1 = NSSelectorFromString(@"test");
    
    // 将SEL对象转换为NSString对象
    NSString *str = NSStringFromSelector(@selector(test));

字符串格式化:

//04:23===%04zd - 占据4位,空出来的位用0来填补  
self.videoTimeLabel.text = [NSString stringWithFormat:@"%02zd:%02zd", minute, second];

//保留小数点后两位
CGFloat num=3.145;
NSString * str=[NSString stringWithFormat:@"%.2f",num];//3.15

字符串的查找

//字符串的查找
NSString *string23 = @"www.iphonetrain.com/ios.html";
NSRange rang = [string23 rangeOfString:@"ios"];//返回一个范围
if(rang.location == NSNotFound){
}
else{}

字符串的截取:

   NSString *address = @"www.baidu.com";
   
   NSString *str1 = [address substringToIndex:3];//从开始位置截取到下标为3的字串(不包括3) www
   
   NSString *str2 = [address substringFromIndex:4];//从index开始截取到尾部(包括3)  baidu.com
   
   NSRange range = {4,address.length-4};
   NSString *str3 = [address substringWithRange:range];//截取1~(3+1)部分的字串  baidu.com

字符串的分割:

    //字符串转换成数组:
    NSString * str = @"www.itacast.cn";
    NSArray * strArray = [str componentsSeparatedByString:@"."];
    NSLog(@"strArray %@",strArray);

判断字符串的开头和结尾

    //判断字符串是否以指定的内容开头
    NSString * str2 = @"www.baidu.com";
    BOOL isPerfix = [str2 hasPrefix:@"www"];
    
    //判断字符串是否以指定的内容结尾;在开发中中常用来判断文件格式
    BOOL isSuffix = [str2 hasSuffix:@".txt"];

Trim操作

    NSString *str = @"  www.baidu.com   ";
    NSString *str2 = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

读取文件内容

    //你写入文件的时候,使用的是什么编码格式,读取的时候就要给我用什么编码格式
    NSString * path = @"/Users/aplle/Desktop/mytest.txt";
    NSError * error;
    NSString * str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];

字符串的替换

//使用新的字符,替换原有的字符
NSString * str5 = @"www.baidu.cn";
NSString * str6 = [str5 stringByReplacingOccurrencesOfString:@"cn" withString:@"com"];

//在字符串中删除特定的某些字符 胡汉三 hhs
NSString * str7 = @"www---.baidu.--cn";
NSString * str8 = [str7 stringByReplacingOccurrencesOfString:@"-" withString:@""];


//去掉字符串中的空格
NSString * str9 = @"www  .baidu  .cn";
NSString * str10 = [str9 stringByReplacingOccurrencesOfString:@" " withString:@""];

字符串的追加

//在指定的范围追加字符串
NSString * str3 = @"www.cn";
NSRange range = {4,0};
//location代表的是从哪个索引开始插入,length 代表的意思可以覆盖几个字符
NSString * str4 = [str3 stringByReplacingCharactersInRange:range withString:@"baidu."];

//字符串的末尾追加新的字符
NSString * str1 = @"www.";
NSString * str2 = [str1 stringByAppendingString:@"baidu.com"];
上一篇下一篇

猜你喜欢

热点阅读