NSString 演示(无注释)

#import <Foundation/Foundation.h>

#import <objc/objc.h>

#import <objc/objc-runtime.h>

void stringTest(void)

{

    /*

    NSString *str = @"hello world";//常量字符串

    NSString *str1 = [[NSString alloc] init];

    NSString *str2 = [[NSString alloc] initWithFormat:@"%s//www.mobiletrain.org/pic/%d.png", "http:", 1000];

    NSString *str3 = [[NSString alloc] initWithString:str2];

    NSString *str4 = [[NSString alloc] initWithUTF8String:"asfdhjashf"];

     */

    NSString *str1 = [NSString stringWithFormat:@"%s:%d", "asdf", 10];

    NSString *str2 = [NSString stringWithContentsOfFile:@"/etc/passwd" encoding:NSUTF8StringEncoding error:nil];

    NSLog(@"%@", str1);

}


void stringTest2(void)

{

    //NSLog(@"c len= %u, ns len=%d", strlen("中国"), [@"中国" length]);

    unichar c = [@"23" characterAtIndex:0];//取出指定位置的字符

    NSLog(@"%d", c);

    NSString *str = @"abcdef";

    unichar buf[100] = {0};

    [str getCharacters:(unichar *)buf];

    NSLog(@"%c, %c, %c, %c, %c, %c", buf[0],buf[1], buf[2], buf[3],

          buf[4], buf[5]);

}

void stringTest3(void)

{

    NSString *str = @"abcdefghijklmnopqrstuvwxyz";

    NSLog(@"%@", [str substringFromIndex:8]);

    NSLog(@"%@", [str substringWithRange:NSMakeRange(5, 10)]);

    

}

void stringTest4(void)

{

    NSString *str = @"abcde";

    NSLog(@"%ld", [str compare:@"Abcde" options:NSCaseInsensitiveSearch]);

}

void stringTest5(void)

{

    NSRange r = [@"abcdesf" rangeOfString:@"CDE" options:NSCaseInsensitiveSearch];//rangeOfString 前面的参数是要被搜索的字符串,后面的是要搜索的字符

    if (r.location == NSNotFound) {

        NSLog(@"not found");

    } else

        NSLog(@"%lu, %d", (unsigned long)r.location, (int)r.length);

}

void stringTest6()

{

    NSLog(@"%d, %f", [@"123" intValue], [@"12.35" floatValue]);

    NSLog(@"%@, %@, %@", [@"abcDEF" lowercaseString],

          [@"abcDEF" uppercaseString], [@"ansdfDEF" capitalizedString]);

    

    NSLog(@"%s", [@"ajhsdfhj" cStringUsingEncoding:NSUTF8StringEncoding]);

}

void stringTest7()

{

    NSString *str = @"zhangsan sdh zhangsan askjdfjk zhangsan ashdh";

    NSString *res = [str stringByReplacingOccurrencesOfString:@"zhangsan" withString:@"lisi" options:NSCaseInsensitiveSearch range:NSMakeRange(0, str.length)];

    NSLog(@"%@\n%@", str, res);

    

}

void stringTest8()

{

    NSMutableString *str = [[NSMutableString alloc] initWithCapacity:0];

    [str appendString:@"hello"];

    [str insertString:@"yworld" atIndex:str.length];

    [str appendFormat:@"%s", "!"];

    [str deleteCharactersInRange:NSMakeRange(0, 6)];

    NSLog(@"%@", str);

}

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

{


    @autoreleasepool {

        stringTest3();

        

    }

    return 0;

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值