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
)

 

转载于:https://www.cnblogs.com/lxd2502/p/5464897.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值