记录OC学习的一点一滴(二)

NSString 基础练习:

代码:

//
//  main.m
//  NSStringDemo01
//
//  Created by Levi on 14-3-14.
//  Copyright (c) 2014年 levi. All rights reserved.
//

#import 

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        /*
         NSString
         NSArray
         NSDictionary
         */
        char *s = "Hello World";
        NSString *str1 = @"Hello World";
        NSLog(@"s is %s str1 is %@", s, str1);
        
        
        //NSString *str3 = [[NSString alloc] initWithString:@"Hello World"];
        /* c 类型字符串转化成 oc NSString */
        NSString *str2 = [[NSString alloc] initWithCString:s encoding:NSUTF8StringEncoding];
        const char *p = [str2 UTF8String];
        NSLog(@"str2 is %@ p is %s", str2, p);
        
        
        NSString *str3 = [NSString stringWithFormat:@"%s%d%c%f", "sss", 10, 'c', 3.14];
        NSLog(@"str3 is %@", str3);
        
        
        NSString *str4 = @"10";
        int a = str4.intValue;
        NSLog(@"a is %d", a);
        NSLog(@"strlen is %ld", str3.length);
        
        
        NSString *str5 = @"wahaha";
        NSString *str6 = @"wahaha";
        NSLog(@"cmp : %d", [str5 isEqualToString:str6]);
        
        
        NSString *s1 = @"My name is";
        NSString *s2 = @"Levi";
        NSString *s3 = @"I am 24";
        NSString *s4 = [NSString stringWithFormat:@"%@ %@ %@", s1, s2, s3];
        NSLog(@"%@", s4);
        
        
        NSString *s5 = @"145";
        NSString *s6 = @"151";
        int result = [s5 compare:s6];
        NSString *s7 = @"NSOoerdefadfLeviadlfkj, usidfja, dfdfddl";
        NSRange range = [s7 rangeOfString:@"td"];
        /* 判断条件 */
        NSLog(@"NSNotFount is %ld", NSNotFound);
        
        
        if(range.location == NSNotFound && range.length == 0)
            NSLog(@"NO found!");
        else NSLog(@"location is %ld length is %ld", range.location, range.length);
        NSString *s8 = @"Welcome to Bejing";
        NSRange range2 = {3, 6};
        NSString *s9 = [s8 substringWithRange: range2];
        NSLog(@"%@", s9);
        
        
        NSString *s10 = [s8 substringFromIndex:s8.length-6];
        NSLog(@"%@", s10);
        
        
        NSString *s11 = @"When I was young, I loved a girl on neighbor class.";
        NSRange range3 = [s11 rangeOfString:@"was"];
        NSRange range4 = [s11 rangeOfString:@"girl"];
        NSRange range5;
        range5.length = range4.location - range3.location + range4.length;
        range5.location = range3.location;
        NSString *s12 = [s11 substringWithRange:range5];
        NSLog(@"%@", s12);
        
        NSString *s13 = @"Welcome to Qianfeng";
        NSRange range6 = [s13 rangeOfString:@"to"];
        NSString *s15 = [s13 substringToIndex:range6.location-1];
        NSString *s16 = [s13 substringFromIndex:range6.location+range6.length];
        NSString *s14 = [NSString stringWithFormat:@"%@%@", s15, s16];
        NSLog(@"%@", s14);
        
        NSString *s17 = @"Hello world";
        NSRange range7 = [s17 rangeOfString:@"Hello"];
        NSRange range8 = [s17 rangeOfString:@"world"];
        NSString *s19 = [s17 substringWithRange:range7];
        NSString *s20 = @"qianfeng";
        NSString *s21 = [s17 substringWithRange:range8];
        NSString *s18 = [NSString stringWithFormat:@"%@ %@ %@", s19, s20, s21];
        NSLog(@"%@", s18);
        
        NSString *s22 = @"When I was young, I loved a girl on neighbor class. ";
        NSRange range9 = [s22 rangeOfString:@"young"];
        NSRange range10 = [s22 rangeOfString:@"girl"];
        NSString *s23 = [s22 substringToIndex:range9.location];
        NSString *s24 = [s22 substringFromIndex:range10.location+range10.length];
        NSString *s25 = @"teacher, I rather teaching the students";
        NSString *s26 = [NSString stringWithFormat:@"%@%@%@", s23,s25,s24];
        NSLog(@"%@", s26);
    }
    return 0;
}

  

转载于:https://www.cnblogs.com/firstrate/p/3601165.html

你可能感兴趣的:(记录OC学习的一点一滴(二))