NSString的操作,下面记录下他的常用方法:

 

NSString *str = @"1111111111aaaaaaaaa2222222222";

 

1、查找

//查找
    NSRange range = [str rangeOfString:@"aaa"];
    if (range.location == NSNotFound) {
        NSLog(@"str中没有找到aaa");
    }else{
        NSLog(@"str中包含");
    }
    //另一种方法
    if (range.length == 0) {
        NSLog(@"str中没有找到aaa");
    }else{
        NSLog(@"str中包含");
    }

比较的方法:

[value compare:(NSString *)];

[value compare:(NSString *) options:(NSStringCompareOptions)];

[value compare:(NSString *) options:(NSStringCompareOptions) range:(NSRange)];

这里说下options属性,这个是个枚举,如下:

enum{

    NSCaseInsensitiveSearch = 1,//不区分大小写比较

    NSLiteralSearch = 2,//区分大小写比较

    NSBackwardsSearch = 4,//从字符串末尾开始搜索

    NSAnchoredSearch = 8,//搜索限制范围的字符串

    NSNumbericSearch = 64//按照字符串里的数字为依据,算出顺序。例如 Foo2.txt < Foo7.txt < Foo25.txt

//以下定义高于 mac os 10.5 或者高于 iphone 2.0 可用

    NSDiacriticInsensitiveSearch = 128,//忽略 "-" 符号的比较

    NSWidthInsensitiveSearch = 256,//忽略字符串的长度,比较出结果

    NSForcedOrderingSearch = 512//忽略不区分大小写比较的选项,并强制返回 NSOrderedAscending 或者 NSOrderedDescending

//以下定义高于 iphone 3.2 可用
    NSRegularExpressionSearch = 1024//只能应用于 rangeOfString:..., stringByReplacingOccurrencesOfString:...和 replaceOccurrencesOfString:... 方法。使用通用兼容的比较方法,如果设置此项,可以去掉 NSCaseInsensitiveSearch 和 NSAnchoredSearch

}

返回值:

返回值:

typedef enum _NSComparisonResult {
     NSOrderedAscending = -1,    // < 升序
     NSOrderedSame,              // = 等于
     NSOrderedDescending   // > 降序
} NSComparisonResult;

使用如下:

BOOL result;
 result = [str compare:@"444"];
 [str compare:@"333" options:NSCaseInsensitiveSearch];
 [str compare:@"555" options:NSLiteralSearch range:range];

 

2、截取操作

//截取字符串
    //截取,loc标示第几个字符开始,len标示截取两个字符
    NSString *bStr = [str substringWithRange:NSMakeRange(0, 2)];
    NSLog(@"%@",bStr);
    //从第n+1为开始截取,至字符串最后
    NSString *bStr1 = [str substringFromIndex:0];
    NSLog(@"%@",bStr1);
    //从首字母开始截取第n个字符
    NSString *bStr2 = [str substringToIndex:3];
    NSLog(@"%@",bStr2);

3、拆分

//拆分
    //从字符串8中分割成2个元素的数组  这个方法可以实现字符串和数组直接互转
    NSArray *array = [str componentsSeparatedByString:@"8"];
    NSLog(@"array:%@",array);
    NSLog(@"ary1 is %@",[array firstObject]);
    NSLog(@"ary2 is %@",[array lastObject]);

4、替换

NSString *str1 = @"123456789";
    NSString *replacedStr = [str1 stringByReplacingOccurrencesOfString:@"345" withString:@"想要替换成字符串"];
    NSLog(@"erplacedStr === %@",replacedStr);

5、插入

//插入  只能对可变字符串操作,下面insert方法,依次是原字符串,插入的字符,插入的位置
    NSMutableString *currentStr = [NSMutableString stringWithFormat:@"%@",@"豫A88888"];
    NSMutableString *insertStr = [[NSMutableString alloc]initWithString:@"-"];
    [currentStr insertString:insertStr atIndex:2];
    NSLog(@"currentstr is %@",currentStr);

6、验证

//验证,包含的前标和后标
    NSString *str2 = @"http:www.baidu.com";
    BOOL a = [str2 hasPrefix:@"http"];//前缀
    BOOL b = [str2 hasSuffix:@"com"];//后缀
    NSLog(@"a===%@,b===%@",a ?@"YES":@"NO",b ?@"YES":@"NO");

7、大小写互转

//改变字符串的大小写
    NSString *str3 = @"zhengXZHOU";
    NSString *da = [str3 uppercaseString];//转成大写
    NSString *xiao = [str3 lowercaseString];//转成小写
    NSString *shouDa = [str3 capitalizedString];//首字母大写,其余小写
    NSLog(@"\n%@\n%@\n%@",da,xiao,shouDa);

8、编码   解码

//编码   解码
    //编码
    NSString *hStr = @"你好啊";
    NSString *uniStr = [NSString stringWithUTF8String:[hStr UTF8String]];
    NSData *uniData = [uniStr dataUsingEncoding:NSNonLossyASCIIStringEncoding];
    NSString *goodStr = [[NSString alloc] initWithData:uniData encoding:NSUTF8StringEncoding] ;
    NSLog(@"goodstr is %@",goodStr);
    
    //解码
    const char *jsonstr = [goodStr UTF8String];
    NSData *jsonData = [NSData dataWithBytes:jsonstr length:strlen(jsonstr)];
    NSString *goodMsg = [[NSString alloc]initWithData:jsonData encoding:NSNonLossyASCIIStringEncoding];
    NSLog(@"goodmsg is %@",goodMsg);

 

下面附上相应的打印:

ios开发之--NSString的操作_字符串

仅做记录!


作者:稻草人11223