首页 > 代码库 > OC 字符串常用方法总结

OC 字符串常用方法总结

#import <Foundation/Foundation.h>#pragma mark 初始化字符串void initStr() {    //1.init    NSString *str1 = [[NSString alloc ]init];//不可改变的空字符串 无意义    NSString *str2 = [NSString string];    NSLog(@"%@-----%@", str1, str2);    //2    NSString *str3 = [[NSString alloc] initWithFormat:@"%iAA",2];    NSString *str4 = [NSString stringWithFormat:@"%iBB", 3];    NSLog(@"%@-----%@", str3, str4);    //3快捷方式    NSString *str5 = @"QWE";    NSLog(@"%@", str5);    }#pragma mark 从文件中获取字符串void getStrFromFile() {    NSString *str = [NSString stringWithContentsOfFile:@"/Users/lanou3g/Desktop/a.txt" encoding:NSUTF8StringEncoding error:nil];    NSLog(@"%@", str);}#pragma mark 字符串写入文件void setStrInFile() {    NSString *str = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ";    NSError *error ;    [str writeToFile:@"/Users/lanou3g/Desktop/a.txt" atomically:YES encoding:NSUTF8StringEncoding error:&error];    if (error)        NSLog(@"文件写入失败%@", [error localizedDescription]);    else        NSLog(@"文件写入成功");}#pragma mark 获取字符串长度void getStrLength() {    NSString *str = @"赵延伟";    str = @"AA";    NSInteger length = [str length];//1返回的字符个数    NSLog(@"length = %zi", length);//3        length = [str lengthOfBytesUsingEncoding:NSUTF8StringEncoding];//2 返回指定编码的字节数 (针对汉字)    NSLog(@"length = %zi", length);//9        length = [str maximumLengthOfBytesUsingEncoding:NSUTF8StringEncoding];//3 返回字节存储接收器在给定的编码所需的最大数目(不知所云)    NSLog(@"length = %zi", length);}#pragma mark 获取字符void getChar() {    NSString *str = @"ABC";    char ch = [str characterAtIndex:1];//根据索引获取字符 ch = c     //  [str getCharacters:‘C‘ range:NSMakeRange(0, 1)];//???            NSLog(@"%c",ch);}##pragma mark 获取C语言的字符串void getCStr() {    NSString *str = @"AAAABBC";    const char *ch1 = [str UTF8String];//不可变的    NSLog(@"%s",ch1);      //  BOOL b= [str getCString:"12" maxLength:7 encoding:NSUTF8StringEncoding];不明确        const char *ch2 = [str cStringUsingEncoding:NSUTF8StringEncoding];    NSLog(@"%s",ch2);    //NSLog(@"%@", str);    }#pragma mark 拼接字符串void appendStr() {    NSString *str = @"AAA";    NSString *temp = @"BB";    //1    NSLog(@"%@", [str stringByAppendingString:temp]);    //2    NSLog(@"%@", [str stringByAppendingFormat:@"%iKFC%.2f", 8, 3.14]);    //3 参数说明 第一个返回的字符串长度 第二个是 追加的字符串 第三个是追加字符串的索引值    str = [str stringByPaddingToLength:9 withString:@"233" startingAtIndex:2];    NSLog(@"%@", str);}#pragma mark 拆分字符串void didStr() {    NSString *list = @"Karin, Carrie, David";    NSArray *listItems = [list componentsSeparatedByString:@", "];    NSLog(@"%@",listItems);        NSString *str = @"ABCDEFGHI";    str = [str substringFromIndex:2];//包括当前位置        NSLog(@"%@",str);    str = [str substringToIndex:6];//不包括当前位置       NSLog(@"%@",str);    str = [str substringWithRange:NSMakeRange(1, 3)];        NSLog(@"%@",str);}#pragma mark 查找字符串void findStr() {    NSString *str = @"1234AAA5678";    NSRange range = [str rangeOfString:@"AAA"];    NSLog(@"%@", NSStringFromRange(range));        range = [str rangeOfString:@"AAA" options:1];//1表示并行    NSLog(@"%@", NSStringFromRange(range));        range = [str rangeOfString:@"AAA" options:1 range:NSMakeRange(0, 6)];    NSLog(@"%@", NSStringFromRange(range));//不存在时候 判断 range.length == 0}#pragma mark 替换字符串void replaceStr() {    NSString *str = @"1234AAA5678";    str = [str stringByReplacingOccurrencesOfString:@"AAA" withString:@"BBB"];    str = [str stringByReplacingCharactersInRange:NSMakeRange(6, 3) withString:@"CCC"];    NSLog(@"%@", str);}int main(int argc, const char * argv[]){    @autoreleasepool {        //initStr();        //getStrFromFile();        //setStrInFile();        //getStrLength();        //getChar();        //getCStr();        //appendStr();                //didStr();       // findStr();        replaceStr();        //NSArray;                    }    return 0;}

 

OC 字符串常用方法总结