字符串操作

#import 
#import "NSArray+Log.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        // 1.实例化字符串对象
        NSString *str = @"OC字符串";
        
        // 2.获取字符串长度
        NSUInteger len = str.length;
        NSLog(@"%lu",len);
        
        // 3.获取某个位置的字符
        unichar r = [str characterAtIndex:0];
        NSLog(@"%c",r);
    
        
        // 4.将char类型转换为OC字符串类型
        NSString *ss = [NSString stringWithFormat:@"%c",r];
        
        // 5.获取字符的范围
        NSRange range = [str rangeOfString:ss];
        NSLog(@"---%@",NSStringFromRange(range));
        
        // 6.获取一个范围的字符串
        NSRange range2 = {0,2};
        NSString *strRange = [str substringWithRange:range2];
        NSLog(@"%@",strRange);
        NSLog(@"%@",[str substringFromIndex:2]);
        NSLog(@"%@",[str substringToIndex:2]);
        
        // 7.字符串比较
        BOOL isEqual = [str isEqualToString:@"OC字符串"];
        NSLog(@"%d",isEqual);
        
        // 8.字符串比较方式2
        //        NSLog(@"%ld", (long)[str compare:@"OC字符串"]);
        if ([str compare:@"OC字符串"] == NSOrderedSame) {
            NSLog(@"%ld",NSOrderedSame);
        }
        
        // 9.使用字符隔断字符串
        NSArray *array = [str componentsSeparatedByString:@"C"];
        NSLog(@"%@",array);
        
        // 10.查看字符串中是否包含某个字符
        if ([str containsString:(@"字")]) {
            NSLog(@"YES");
        }else {
            NSLog(@"NO");
        }
        
        // 10.字符串替换
        NSString * tr = [str stringByReplacingOccurrencesOfString:@"OC" withString:@"ABC"];
        NSLog(@"%@",tr);
        
        // 11.字符串拼接
        NSString *string1 =  [str stringByAppendingString:@"学习"];
        NSLog(@"%@",string1);
        NSMutableString *mutableStr = [[NSMutableString alloc] initWithString:@"面试"];
        [mutableStr appendString:@"IOS开发"];
        NSLog(@"%@",mutableStr);
        
        // 12.指定索引位置插入字符串
        [mutableStr insertString:@"语言" atIndex:2];
        NSLog(@"%@",mutableStr);
        
        // 13.删除指定范围的字符串
        NSRange range3 = {2,2};
        [mutableStr deleteCharactersInRange:range3];
        NSLog(@"%@",mutableStr);
        
        // 14.字符串转换
        // 1).C 字符串转OC字符串
        const char *s = "c字符串";
        
        // 动态方法
        NSString *str1 = [[NSString alloc] initWithFormat:@"%s",s];
        NSString *str2 = [[NSString alloc] initWithUTF8String:s];
        NSString *str3 = [[NSString alloc] initWithCString:s encoding:NSUTF8StringEncoding];
        
        // 静态方法
        NSString *str4 = [NSString stringWithFormat:@"这是%s创建",s];
        NSString *str5 = [NSString stringWithUTF8String:s];
        NSString *str6 = [NSString stringWithCString:s encoding:NSUTF8StringEncoding];

        NSLog(@"str1 == %@,str2 == %@,str3 == %@,str4 == %@,str5 == %@,str6 == %@",str1,str2,str3,str4,str5,str6);
        
        // 2).OC字符串转换为C字符串
        const char *cString = [str UTF8String];
        const char *cString2 = [str cStringUsingEncoding:NSUTF8StringEncoding];

        NSLog(@"%s",cString);
        NSLog(@"%s",cString2);
        
        // 15.去掉首位空格
        NSString *s2 = @" hello world ";
        s2 = [s2 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        NSLog(@"%@",s2);
        NSString *s3 = [s2 stringByReplacingOccurrencesOfString:@" " withString:@""];
        NSLog(@"%@",s3);

你可能感兴趣的:(字符串操作)