zoukankan      html  css  js  c++  java
  • Object-c字符串操作

    字符串操作:

    -(void) testString{
        NSString *str1 = @"some string";
        NSLog(@"%@", str1);
    
        //格式化字符串
        NSString *str2 = [NSString stringWithFormat:@"%@ %@", @"hello", @123];
        NSLog(@"%@", str2);
    
        //获取以数字开头的字符串的数字
        NSString *str3 = @“124.3abc12";
        double temp1 = [str3 floatValue];
        NSInteger temp2 = [str3 integerValue];
        NSLog(@"%f", temp1);
        NSLog(@"%lu", temp2);
    
        //获取字符串长度
        NSUInteger len = [str3 length];
        NSLog(@"%lu", len);
    
        //比较字符串
        NSString *str4 = @“124.3abC12";
        BOOL isEqual = [str3 isEqualToString:str4];
        NSLog(@"%hhd", isEqual);
        //比较字符串(不区分大小写)
        isEqual = ([str3 caseInsensitiveCompare:str4] == NSOrderedSame);
        NSLog(@"%ld", (long)[str3 caseInsensitiveCompare:str4]);
        NSLog(@"%hhd", isEqual);
    
        //将字符串全部字符转换为大写字符
        str4 = [str4 uppercaseString];
        NSLog(@"%@", str4);
        //将字符串全部字符转换为小写字符
    
        str4 = [str4 lowercaseString];
        NSLog(@"%@", str4);
    
        //去除字符串空字符
        NSString *str5 = @"   one two three   ";
        //去除字符串空字符,包括前后空格,换行符
        str1 = [str5 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        //去除字符串空字符,包括前后空格
    
        str2 = [str5 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        NSLog(@"%@",str1);
        NSLog(@"%@",str2);
    
        //获得substring
        str3 = [str1 substringToIndex:3];
        str4 = [NSString stringWithFormat:@"%@%@",[str2 substringFromIndex:4], @"111" ];
        NSLog(@"%@", str3);
        NSLog(@"%@", str4);
        //分割字符串
        NSArray *arr = [str1 componentsSeparatedByString:@" "];
        NSLog(@"%@", arr);
    }

    对应输出结果:

    2016-05-06 11:09:02.607 test2[65452:7163888] some string
    2016-05-06 11:09:02.607 test2[65452:7163888] hello 123
    2016-05-06 11:09:02.608 test2[65452:7163888] 124.300003
    2016-05-06 11:09:02.608 test2[65452:7163888] 124
    2016-05-06 11:09:02.608 test2[65452:7163888] 10
    2016-05-06 11:09:02.608 test2[65452:7163888] 0
    2016-05-06 11:09:02.608 test2[65452:7163888] 0
    2016-05-06 11:09:02.608 test2[65452:7163888] 1
    2016-05-06 11:09:02.608 test2[65452:7163888] 124.3ABC12
    2016-05-06 11:09:02.608 test2[65452:7163888] 124.3abc12
    2016-05-06 11:09:02.608 test2[65452:7163888] one two three
    2016-05-06 11:09:02.608 test2[65452:7163888] one two three
    2016-05-06 11:09:02.608 test2[65452:7163888] one
    2016-05-06 11:09:02.608 test2[65452:7163888] two three111
    2016-05-06 11:09:02.608 test2[65452:7163888] (
        one,
        two,
        three
    )
  • 相关阅读:
    牛客练习赛51 D题
    Educational Codeforces Round 72 (Rated for Div. 2) C题
    Codeforces Round #583 (Div. 1 + Div. 2, based on Olympiad of Metropolises) C题
    Codeforces Round #583 (Div. 1 + Div. 2, based on Olympiad of Metropolises) A题
    Codeforces Round #583 (Div. 1 + Div. 2, based on Olympiad of Metropolises) A题
    Educational Codeforces Round 72 (Rated for Div. 2) B题
    Educational Codeforces Round 72 (Rated for Div. 2) A题
    《DSP using MATLAB》Problem 7.2
    《DSP using MATLAB》Problem 7.1
    《DSP using MATLAB》Problem 6.24
  • 原文地址:https://www.cnblogs.com/lxd2502/p/5464897.html
Copyright © 2011-2022 走看看