OC字符串操作方法

#**NSString不可变字符串**

1.创建字符串对象

  • 初始化方法

    NSString *str1 = @"ZSY";       
    NSString *str2 = [[NSString alloc] initWithFormat:@"My name is %@", str1];
    
  • 控制台输入一个数字,输出对应苹果型号

    NSLog(@"Please enter a number!");
    char *str = malloc(100);
    scanf("%s", str);
    
  • 使用初始化返回创建字符串对象 能够将C语言的字符串转化为OC中的字符串对象

    NSString *f8string = [[NSString alloc] initWithUTF8String:str];
    NSString *str2 = [[NSString alloc] initWithFormat:@"iPhone%@", f8string];
    NSLog(@"%@", str2);
    
  • 使用便利构造器来创建字符串对象:以类名string开头

    NSString *str3 = [NSString stringWithUTF8String:str];
    NSString *str4 = [NSString stringWithFormat:@"iPhone%@", str3];
    NSLog(@"%@", str4);
    

2.获取字符串长度

NSString *str = [NSString stringWithFormat:@"Lanoukeji"];
NSLog(@"Length = %lu", (unsigned long)str.length);

3.判断字符串是否以http开头、以png结尾,如果是输出是一个有效链接,如果不是输出链接错误

NSLog(@"Please enter a string");
char *p = malloc(100);
scanf("%s", p);
//将C语言的字符串转化为OC中的字符串对象
NSString *str1 = [NSString stringWithUTF8String:p];
//判断字符串头是否是http
BOOL isTrue1 = [str1 hasPrefix:@"http"];
//判断字符串尾是否是png
BOOL isTrue2 = [str1 hasSuffix:@"png"];
if (isTrue1 == YES  && isTrue2 == YES) {
    NSLog(@"是一个有效链接");
} else {
    NSLog(@"链接错误");
}

4.判断两个字符串是否相等

NSString *str1 = @"Lanou";
NSString *str2 = [NSString stringWithFormat:@"Lanou"];
//通过地址判断是否相同
if (str1 == str2) {
    NSLog(@"两个字符串地址相同");
} else {
    NSLog(@"两个字符串地址不同");
}
//通过内容是否相同
//比较两个字符串是否相同是同isEqualToString
if ([str1 isEqualToString:str2]) {
    NSLog(@"两个字符串内容相同");
} else {
    NSLog(@"两个字符串内容不同");
}

5.划分子串

NSString *str1 = [NSString stringWithFormat:@"WenZhang, Er Hun"];
//获取WenZhang
NSString *str2 = [str1 substringToIndex:8];
NSLog(@"%@", str2);
//获取Er Hun
NSString *str3 = [str1 substringFromIndex:10];
NSLog(@"%@", str3);
//获取Zhang
NSMakeRange //快速创建NSRange结构体变量
NSUInteger loc //开始位置
NSUInteger len //长度
NSString *str4 = [str1 substringWithRange:NSMakeRange(3, 5)];
NSLog(@"%@", str4);

6.拼接字符串

NSString *str1 = [NSString stringWithFormat:@"蓝鸥"];
//拼接上海
NSString *str2 = [str1 stringByAppendingString:@"上海"];
NSLog(@"%@", str2);
//输入班级号,拼接到字符串:输入8拼接成:蓝鸥上海8班
NSInteger n = 0;
NSLog(@"Please enter a number");
scanf("%ld", &n);
NSString *str3 = [NSString stringWithFormat:@"%@%ld班", str2, n];
NSLog(@"%@", str3);

7.替换字符串

NSString *str1 = [NSString stringWithFormat:@"LanouShangHai"];
NSString *str2 = [str1 stringByReplacingOccurrencesOfString:str1 withString:@"Leo"];
NSLog(@"%@", str2);

8.练习

给定一个图片文件名,判断字符串中是否以”png”结尾,如果是就替换成”jpg”,如果不是就拼接”.jpg”

NSString *str = [NSString stringWithFormat:@"http://zh.wikipedia.org.png"];
NSString *result = [[NSString alloc] init];
BOOL isTrue = [str hasSuffix:@"png"];
if (isTrue) {
    result = [str stringByReplacingOccurrencesOfString:@"png" withString:@"jpg"];
} else {
    result = [str stringByAppendingString:@".png"];
}
NSLog(@"%@", result);

9.字符串比较

NSString *str1 = [NSString stringWithFormat:@"Frank"];
NSString *str2 = [NSString stringWithFormat:@"Duck"];
NSComparisonResult result = [str1 compare:str2];
NSLog(@"retult = %lu", result);

10.字符串大小写转换

NSString *str = [NSString stringWithFormat:@"lANnou shanghai"];
//全部大写
NSString *upperStr = [str uppercaseString];
NSLog(@"%@", upperStr);
//全部小写
NSString *lowerStr = [str lowercaseString];
NSLog(@"%@", lowerStr);
//单词首字母大写
NSString *capitalStr = [str capitalizedString];
NSLog(@"%@", capitalStr);

11.字符串与数值类型转换

  • 基本数据类型,转换为字符串

    int number = 324;
    NSString *str = [NSString stringWithFormat:@"%d", number];
    NSLog(@"%@", str);
    float number2 = 10.7;
    NSString *str1 = [NSString stringWithFormat:@"%f", number2];
    NSLog(@"%@", str1);
    
  • OC中的字符串转化为基本数据类型
    • int类型

      NSString *str = [NSString stringWithFormat:@"20"];
      int number = [str intValue];
      NSLog(@"%d", number);
      
    • float类型

      NSString *str1 = [NSString stringWithFormat:@"20.222"];
      float number2 = [str1 floatValue];
      NSLog(@"%.3f", number2);
      
    • long类型

      NSString *str2 = [NSString stringWithFormat:@"1000000000000"];
      long number3 = [str2 integerValue];
      NSLog(@"%ld", number3);
      

NSMutableString可变字符串

1.创建可变字符串对象

可变字符串是在不可变字符串基础上增加了对原有对象增删改的操作,而不可变字符串操作的是副本,本身是不可变的

创建可变字符串对象:capacity —- 大小

NSMutableString *str = [[NSMutableString alloc] initWithCapacity:1];
NSMutableString *str2 = [NSMutableString stringWithCapacity:1];

2.修改字符串(对原有字符串对象)

  • 拼接字符串
NSMutableString *str3 = [NSMutableString stringWithFormat:@"LanouShangHaiKeJi"];
[str3 appendFormat:@"Leo%d", 1];
NSLog(@"%@", str3);
[str3 appendString:@"Frank"];
NSLog(@"%@", str3);
//当子类str3使用父类不可变字符串的方法时,可变字符串str3按照不可变字符串方法操作,不改变原串
NSString *str4 = [str3 stringByAppendingString:@"Duck"];
NSLog(@"%@", str3);
NSLog(@"%@", str4);
  • 插入字符串
[str3 insertString:@"AAAAA" atIndex:str3.length];
NSLog(@"%@", str3);
  • 替换
NSMakeRange(NSUInteger loc, NSUInteger len)
//loc代表从哪个位置替换  len标识替换原对象多少个字符
[str3 replaceCharactersInRange:NSMakeRange(3, 10) withString:@"4444444444444444444"];
NSLog(@"%@", str3);
  • 重置字符串
[str3 setString:@"ZSY"];
NSLog(@"%@", str3);

NSNumber

1.将基本数据类型转化成NSNumber数值对象

将char类型转换为NSnumber类型

char number1 = 'm';
NSNumber *charNumber = [NSNumber numberWithChar:number1];

将int类型转换为NSnumber类型

int number2 = 10;
NSNumber *intNumber = [NSNumber numberWithInt:number2];

将BOOL类型转换为NSnumber类型

BOOL number3 = YES;
NSNumber *boolNumber = [NSNumber numberWithBool:number3];

将sort类型转换为NSnumber类型

short number4 = 15;
NSNumber *shortNumber = [NSNumber numberWithShort:number4];

将long类型转换为NSnumber类型

long number5 = 100;
NSNumber *longNumber = [NSNumber numberWithLong:number5];

2.将NSNumber数值对象转换为基本数据类型

将NSNumber类型转换为int类型

NSNumber *num1 = @20;
int intNum = [num1 intValue];

将NSNumber类型转换为BOOL类型

NSNumber *num2 = @NO;
BOOL boolNum = [num2 boolValue];

将NSNumber类型转换为char类型

NSNumber *num3 = @'m';
char charNum = [num3 charValue];

3.比较

NSNumber *numb1 = @20;
NSNumber *numb2 = @100;
NSComparisonResult result = [numb1 compare:numb2];

NSValue 处理自定义数据类型(结构体)、指针等

Student stu = {"ZSY", "Man", 25};
//把结构体变量封装成NSvalue对象
NSValue *value = [NSValue valueWithBytes:&stu objCType:@encode(Student)];
Student stu1 = {0};
[value getValue:&stu1];
NSLog(@"%s, %s, %d", stu1.name, stu1.gender, stu1.age);
阅读更多
换一批

没有更多推荐了,返回首页