Foundation框架提供的一些基本对象的用法之字符串对象

来源:互联网 发布:游戏攻略软件 编辑:程序博客网 时间:2024/05/22 14:24
创建一个字符串对象最简单的方法:使用一对双引号将一组字符串引起来,例如@“Programming is fun”
一、不可变字符串对象(NSString)的常用方法
  NSString *str1 = @"This is string A";
        NSString *str2 = @"This is string B";
        NSString *res;
        NSComparisonResult compareResult;// 结构体

        //计算字符串中的字符
        NSLog(@"Length of str1:%lu",[str1 length]);

        // 将一个字符串复制到另一个字符串(深复制)
        res = [NSString stringWithString:str1];
        
        // 将一个字符串复制到另一个字符串末尾
        str2 =[str1 stringByAppendingString:str2];
    
        // 验证两个字符串是否相等
        if ([str1 isEqualToString:res] == YES)
            NSLog(@"str1 == res");
        else
            NSLog(@"str1 != res");

        // 验证一个字符串是否小于、等于或大于另一个字符串。如果不是敏感比较可以用这个方法: caseInsensitiveCompare:这个方法比较不区分大小写
        compareResult = [str1 compare:str2];

        if (compareResult == NSOrderedAscending) {
            NSLog(@"str1 < str2”);iii
        }else if (compareResult == NSOrderedSame){
            NSLog(@"str1 = str2");
        }else{// compareResult == NSOrderedDescending
            NSLog(@"str1 > str2");
        }

        // 将字符串装换为大写,并不影响原始的字符串
        res = [str1 uppercaseString];
        NSLog(@"Uppercase conversion:%s",[res UTF8String]);

        // 将字符串装换为小写
        res = [str1 lowercaseString];
        NSLog(@"Uppercase conversion:%@",res);


        NSString *str1 = @"This is string A";
        NSString *str2 = @"This is string B";
        NSString *res;
        NSRange subRange;// 结构体

        // 从字符串中提取前3个字符
        res = [str1 substringToIndex:3];
        NSLog(@"First 3 chars of str1:%@",res);

        // 提取从索引5开始知道结尾的子字符串
        res = [str1 substringFromIndex:5];
        NSLog(@"Chars from index 5 of str1 :%@",res);

        // 提取从索引8开始到索引13的子字符串
        res = [[str1 substringFromIndex:8] substringToIndex:6];
        NSLog(@"Chars from index 8 through 13: %@",res);

        // 更简单的方法
        res = [str1 substringWithRange:NSMakeRange(8, 6)];
        NSLog(@"Chars from index 8 through 13: %@",res);

        // 从另一个字符串中查找一个字符串
        subRange = [str1 rangeOfString:@"string A"];
        NSLog(@"String is at index %lu,lenth is %lu",subRange.location,subRange.length);

        subRange = [str1 rangeOfString:@"string B"];
        if (subRange.location == NSNotFound) {
            NSLog(@"String not found");
        }else{
            NSLog(@"String is at index %lu,length is %lu",subRange.location,subRange.length);
        }
其他常用方法:
+ (instancetype)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error
创建一个新字符串,并将其设置为path指定的文件内容,使用字符编码enc,如果非零,则返回error中的错误
- (instancetype)initWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error
实例方法,作用同上

+ (instancetype)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error
创建一个新字符串,并将其设置为url的内容,使用字符编码enc,如果非零,则返回error中的错误
- (instancetype)initWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error
实例方法,作用同上

+ (instancetype)string  创建一个新的空字符串
+ (instancetype)stringWithFormat:(NSString *)format, …      通过指定的format和参数创建一个新字符串
- (instancetype)initWithString:(NSString*)string  实例方法创建string
-(BOOL)hasPrefix:(NSString*)string 测试字符串是否以string开始
-(BOOL)hasSuffix:(NSString*)string 测试字符串是否以string结尾
-(NSString *)capitalizedString  返回每个单词首字母大写的字符串(每个单词的其余字母转换为小写)
-(const char*)UTF8String  返回转换为UTF-8 C样式的字符串
-(double)doubleValue  返回转换为double的字符串
-(float)floatValue  返回转换为float的字符串
-(int)intValue  返回转换为int的字符串
-(NSInteger)integerValue  返回字符串的NSInteger整数表示

二、可变字符串对象(NSMutableString)的常用方法
NSMutableString类可以用来创建可更改字符的字符串对象。因为它是NSString类的子类,所以可以使用NSString类的方法。

       NSString *str1 = @"This is string A";
        NSString *search,*replace;
        NSMutableString *mstr;
        NSRange substr;

        // 从不可变字符串中创建可变字符串
        mstr = [NSMutableString stringWithString:str1];

        // 插入字符(没有返回值,因为可变字符串)
        [mstr insertString:@" mutable" atIndex:7];

        // 插入末尾进行有效拼接
        [mstr insertString:@" and string B" atIndex:[mstr length]];

        // 直接使用appendString
        [mstr appendString:@" and string C"];

        // 根据范围删除子字符串
        [mstr deleteCharactersInRange:NSMakeRange(16, 13)];

        // 查找然后将其删除
        substr = [mstr rangeOfString:@"sting B and"];
        if (substr.location != NSNotFound) {
            [mstr deleteCharactersInRange:substr];
        }

        // 直接设置为可变的字符串
        [mstr setString:@"This is string A"];

        // 替换一些字符
        [mstr replaceCharactersInRange:NSMakeRange(8, 8) withString:@"a mutable string"];

        // 查找和替换
        search = @"This is";
        replace = @"An example of";
        substr = [mstr rangeOfString:search];
        if (substr.location != NSNotFound) {
            [mstr replaceCharactersInRange:substr withString:replace];
        }

        // 查找和替换所有匹配项
        search = @"a";
        replace = @"x";
        substr = [mstr rangeOfString:search];
        while (substr.location != NSNotFound) {
            [mstr replaceCharactersInRange:substr withString:replace];
            substr = [mstr rangeOfString:search];
        }
        // 上面的代码,可以用下面的方法替代(可避免无限循环)
        [mstr replaceOccurrencesOfString:search withString:replace options:nil range:NSMakeRange(0, [mstr length])];

其他常用方法:
+ (NSMutableString *)stringWithCapacity:(NSUInteger)capacity  创建一个初始包含capacity字符的字符串
- (NSMutableString *)initWithCapacity:(NSUInteger)capacity  实例方法,作用同上
0 0
原创粉丝点击