首页 > 代码库 > OC中的那些String

OC中的那些String

/--------操作字符串--NSString(静态字符串)---------------------

NSString * a = @"a+b+c+a+b+d";

        NSArray  * m = [a componentsSeparatedByString:@"+"];//字符串根据某个字串拆分

NSRange x = [a rangeOfString:@"b"];//查找子字符串在总字符串的范围

        NSLog(@"%lu, %lu", x.location , x.length);

NSString * r = [a stringByReplacingOccurrencesOfString:@"+" withString:@"-"];//把某个字符串替换成另一个字符串

        NSLog(@"%@", r);

NSString * str = @"wo shi xiao hong jun!";

        NSString * result = [str uppercaseString];//小写字母变成大写

        NSLog(@"%@", result);

        NSString * result1 = [str capitalizedString];//第一个单词变成大写

        NSLog(@"%@", result1);

        NSString * result2 = [result lowercaseString];//大写字母改为小写

        NSLog(@"%@", result2);


    NSString * Beijing= @"河南";        //字符串的声明

    NSString * log=@"河南欢迎您a"; //[NSString stringWithFormat:@"I am ‘%@‘", Beijing];     //字符串格式化

    NSString * zhui = [Beijing stringByAppendingString:@"啦啦啦"];        //字符串追加

    bool b=[Beijing isEqualToString:log];                               //字符串比较

    NSString * hh = @"http://www.cnblog.com";

    if([hh hasPrefix:@"http"]){                                          //查找以http开头的字符串

        NSLog(@"含有http");

    }else{

        NSLog(@"没有http");
    }
    NSString * ss = @"123";

    int a = [ss intValue];                                   //字符串转int型

    double dd = [ss doubleValue];                          //字符串转double型

    NSLog(@"%g", dd);

//字符串转数组 

    NSString * zifuchuan =@"one, two, three, four";

    NSLog(@"string:%@", zifuchuan);

    NSArray * array = [zifuchuan componentsSeparatedByString:@","];//用,把字符分成数字元素

//    NSLog(@"array:%@", array);                             //输出整个数组中所有元素 

    NSString * value = http://www.mamicode.com/[array objectAtIndex:0]; //取出第0个元素

    NSLog(@"value:%@", value);

//数组转字符串

    NSString * zifuchuan2 = [array componentsJoinedByString:@","];//用,把各个数字组成字符串

    NSLog(@"zifuchuan2:%@", zifuchuan2);
       
//-substringToIndex: 从字符串的开头一直截取到指定的位置,但不包括该位置的字符

NSString * string1 = @"This is a string";

NSString * string2 = [string1 substringToIndex:3];

NSLog(@"string2:%@",string2);

//-substringFromIndex: 以指定位置开始(包括指定位置的字符),并包括之后的全部字符

NSString * string1 = @"This is a string";

NSString * string2 = [string1 substringFromIndex:3];

NSLog(@"string2:%@",string2);


//-substringWithRange: //按照所给出的位置,长度,任意地从字符串中截取子串

NSString * string1 = @"This is a string";

NSString * string2 = [string1 substringWithRange:NSMakeRange(0, 4)];//0带表下标,4表示长度

NSLog(@"string2:%@",string2);

//--------操作动态字符串--NSMutableString----------------------------------------------------

    NSMutableString * mstr = [[NSMutableString alloc] init];

    NSString * str1 = @"This is a example.";

    //创建可变字符串

    mstr = [NSMutableString stringWithString:str1];


//插入字符

    [mstr insertString:@"very easy " atIndex:10];


    //删除一些字符

    [mstr deleteCharactersInRange:NSMakeRange(10,5)];


    //查找并删除

    NSRange substr = [mstr rangeOfString:@"example"];             //字符串查找,可以判断字符串中是否有

    if (substr.location != NSNotFound) {

        [mstr deleteCharactersInRange:substr];

    }

    //重新设置字符串

    [mstr setString:@"This is string AAA"];


    //替换字符串

    [mstr replaceCharactersInRange:NSMakeRange(15, 2) withString:@"BBB"];   //从第15个字符串处替换掉后2个字符串
   
    //查找第一个并替换

    NSString * search = @"This is";

    NSString * replace = @"An example of";

    substr = [mstr rangeOfString:search];

    if (substr.location != NSNotFound) {

        [mstr replaceCharactersInRange:substr withString:replace];      //把第1个遇到的substr替换为replace

        NSLog(@"%@", mstr);
    }
   
    //查找全部匹配的,并替换

    search = @"a";

    replace = @"X";

    substr = [mstr rangeOfString:search];

    while (substr.location != NSNotFound) {

        [mstr replaceCharactersInRange:substr withString:replace];

        substr = [mstr rangeOfString:search];
    }

    NSLog(@"%@", mstr);