天天看点

Objective-C字符串NSString遍历字符串输出一些常见的变量字符串的方法字符串的比较字符串的截取方法字符串的搜索与替换

遍历字符串

NSString * str = @"YUOSOUCANG";

    int count = [str length];

    NSLog(@"%d",count);


    for (int i = ; i < count; i++) {
        char c = [str characterAtIndex:i];

        NSLog(@"字符串的第%d位为%c",i,c);
    }
           

输出一些常见的变量

int a = ;
    double d = l;
    float f = ;
    long l = ;
    short s = ;
    NSLog(@"a = %d, d = %e ,f = %f",a, d, f );

    bool a = NO;
    bool b = NO;
    NSString * str = @"fpc";

    if(a){
        NSLog(@"str = %@",str);
    }else{
        NSLog(@"str = %@",@"no");
    }
           

字符串的方法

NSString * str0 = @"我是傅鹏程";

    NSString * str1 = [NSString stringWithFormat:@"我的名字:%@ 我的年龄 %d 我的邮箱 %s ",@"傅鹏程",,"fpcfpc"];
    //把不同类型的值拼在一起

    NSLog(@"%@",str1);

    NSString * str3 = [str0 stringByAppendingString:str1];
    //字符串的拼接

    NSLog(@"%@",str3);

           

字符串的比较

NSString * str0 = @"傅鹏程momo";

    NSString * str1 = @"傅鹏程momo";

    if ([str0 isEqualToString:str1]) {
        NSLog(@"相同");
    }

    if ([str0 hasPrefix:@"傅"]) {
        NSLog(@"开头");
    }

    if ([str0 hasSuffix:@"momo"]) {
        NSLog(@"结尾");
    }
           

字符串的截取方法

NSString * stro = @"中文my name is 傅鹏程";

    NSString * to = [stro substringToIndex:];
    //从起始位置到目标位置
    NSLog(@"to = %@",to);
    //to = 中文my


    NSString * from = [stro substringFromIndex:];
    //从目标位置到结尾

    NSLog(@"from = %@", from);
    //from = name is 傅鹏程



    NSRange rang = NSMakeRange(, );

    NSString * strRang = [stro substringWithRange:rang];
    //选取范围
    NSLog(@"strRang = %@",strRang);
    //strRang = y name



    //字符串的其他方法
    NSLog(@"字符串的首字母大写:%@",[[stro substringWithRange:rang] capitalizedString]);

    NSLog(@"把字符串都转成大写:%@",[stro uppercaseString]);

    NSLog(@"把字符串都转成小写:%@",[stro lowercaseString]);


//    2017-10-12 09:12:40.761 app1[1670:75841] to = 中文my
//    2017-10-12 09:12:40.761 app1[1670:75841] from = name is 傅鹏程
//    2017-10-12 09:12:40.761 app1[1670:75841] strRang = y name
//    2017-10-12 09:12:40.762 app1[1670:75841] 字符串的首字母大写:Y Name
//    2017-10-12 09:12:40.762 app1[1670:75841] 把字符串都转成大写:中文MY NAME IS 傅鹏程
//    2017-10-12 09:12:40.762 app1[1670:75841] 把字符串都转成小写:中文my name is 傅鹏程
           

字符串的搜索与替换

NSString * str0 = @"中文my name is fupengcheng";

    NSString * temp = @"is";

    NSRange rang = [str0 rangeOfString:temp];

    NSLog(@"搜索到的字符串的起始位置: %d", rang.location);

    NSLog(@"搜索到的字符串的结束位置:%d", rang.location + rang.length);


    NSString * str1 = [str0 stringByReplacingCharactersInRange:rang withString:@"傅鹏程"];

    NSLog(@"替换后的字符串为:%@",str1);


    str1 = [str0 stringByReplacingOccurrencesOfString:@" " withString:@"@"];

    NSLog(@"替换后的字符串为:%@",str1);


//    2017-10-12 09:29:07.304 app1[1851:84555] 搜索到的字符串的起始位置: 10
//    2017-10-12 09:29:07.304 app1[1851:84555] 搜索到的字符串的结束位置:12
//    2017-10-12 09:29:07.304 app1[1851:84555] 替换后的字符串为:中文my name 傅鹏程 fupengcheng
//    2017-10-12 09:29:07.304 app1[1851:84555] 替换后的字符串为:中文[email protected]@[email protected]