Objective-C个人学习系列(1) NSString NSMutableString

// 字符初始化

void initStaticString(){

 

    NSString *s1 = @"abc";//[NSString stringWithString:@"abc"];

    NSString *ss1 = [[NSString alloc] initWithString:s1];

    NSString *s2 = [NSString stringWithFormat:@"abcd"];

    NSString *s3 = [NSString stringWithUTF8String:"abcde"];

    NSString *s4 = [NSString stringWithCString:"abc123" encoding:NSUTF8StringEncoding];

    NSString *s5 = [NSString stringWithContentsOfURL:

          [NSURL URLWithString:@"www.baidu.com"]

                encoding:NSUTF8StringEncoding error:nil];

}

// 字符串截取

void mysubString(){

    

    NSString *str=@"abcdefg123456";

    

    //计算字符串长度

    unsigned long n = [str length];

    NSLog(@"%ld",n);

    

    //从第3个位置开始截取,索引从3开始

    NSLog(@"%@",[str substringFromIndex:3]);    //Output:defg123456

    

    //截取到第3个位置,索引到3-1,不包括3

    NSLog(@"%@",[str substringToIndex:3]);      //Output:abc

    

    //截取一段字符串,从2开始的5个字符串

    NSRange range = NSMakeRange(2, 5);   //声明结构体

    NSLog(@"%@",[str substringWithRange:range]);        //Output:cdefg

    //

    NSLog(@"%@",[str substringWithRange:NSMakeRange(2, 5)]);    //Output:cdefg

    

    NSString *str2=@"1,2,3,a,b,c";

    //字符串分割

    NSArray *array=[str2 componentsSeparatedByString:@","];     //Output:(1,2,3,a,b,c)

    NSLog(@"%@",array);

    

    

    NSRange rangeSub = [str rangeOfString:@"defg"];

    //查找子串首字母在父串中的索引位置

    NSLog(@"range.location=%ld",rangeSub.location);     //Output:3

    //查找子串在父串中占的长度,大于0表示找到了,等于0表示没找到

    NSLog(@"range.length=%ld",rangeSub.length);      //Output:4

}

//可变字符串 NSMutableString

void multalbeString(){

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

    

    [mStr appendString:@"This is MutableString!"];

    NSLog(@"%@",mStr);                 //Output:This is MutableString!

    //在原字符串后面添加格式化字符串

    [mStr appendFormat:@"My age is %d\n",23];

    NSLog(@"%@",mStr);              //Output:This is MutableString!My age is 23

    

    //在原字符串指定位置开始插入字符串

    [mStr insertString:@"AAA"atIndex: 8];

    NSLog(@"%@",mStr);              //Output:This is AAAMutableString!My age is 23

    

    //删除指定范围内的一个子字符串

    [mStr deleteCharactersInRange:NSMakeRange(0, 5)];

    NSLog(@"%@",mStr);              //Output:is AAAMutableString!My age is 23

    

    //替换指定范围内的一个子字符串

    [mStr replaceCharactersInRange:NSMakeRange(3, 3) withString:@"BBB"];

    NSLog(@"%@",mStr);              //Output:is BBBMutableString!My age is 23

}

 

void stringOther(){

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

    [mStr appendString:@"abc abc abc WO WO WO!"];

    

    //转换为大写,返回一个大写的新字符串

    NSString* up = [mStr uppercaseString];

    NSLog(@"up=%@",up);

    NSLog(@"mstr=%@",mStr);

    //转换为小写,返回一个小写的新字符串

    NSString* low = [mStr lowercaseString];

    NSLog(@"low=%@",low);

    //返回一个首字母转换成大写的字符串

    NSString* Abc = [mStr capitalizedString];

    NSLog(@"Abc=%@",Abc);

}

//判断前缀后缀

void stringPreSuf(){

    NSString* strURL = @"http://www.music.com/10/love.mp3";

    BOOL isHasPrefix = [strURL hasPrefix:@"http://"];

    BOOL hasSuffix = [strURL hasSuffix:@".mp3"];

    NSLog(@"%d",isHasPrefix);

    NSLog(@"%d",hasSuffix);

}

//字符串比较

void stringComare(){

    NSString* s1 = @"abc";

    NSString* ss1 = @"abc";         //此处ss1 s1 指向的是同一个地址

    NSString* s2 = [[NSString alloc]initWithFormat:@"abc"];

    

    if ([s1 isEqualToString:s2]) {      //比较字符串的值

        NSLog(@"OK!");

    }

    

    if (s1 == s2) {            //比较字符串的地址,s1==s2 为假 s1==ss1 为真

        NSLog(@"OK!");

    }

    int n = [s1 compare:s2];        //1,0,-1

    NSLog(@"%d",n);

}

猜你喜欢

转载自gkuiyj.iteye.com/blog/2040068