首页 > 其他分享 >OC之【NSMutableString的使用】

OC之【NSMutableString的使用】

时间:2022-12-09 15:06:15浏览次数:38  
标签:NSRange OC height NSMutableString range str 使用 字符串

可变字符串的创建
void
// 预先分配10个字数的存储空间
NSMutableString *str = [[NSMutableString alloc] initWithCapacity:10];
// 设置字符串内容
setString:@"1234"];

// 拼接一个字符串
appendString:@"567"];
// 拼接字符串
[str appendFormat:@"age is %i and height is %.2f", 27, 1.55f];

// 替换字符串
NSRange range = [str rangeOfString:@"height"];
//NSRange range = NSMakeRange(7, 3);
[str replaceCharactersInRange:range withString:@"no"];

// 插入字符串
[str insertString:@"abc" atIndex:2];

// 删除字符串
rangeOfString:@"age"];
[str deleteCharactersInRange:range];

NSLog(@"%@", str);

// 释放对象
release];
}

int main(int argc, const char
{
@autoreleasepool {
stringCreate();
}
return 0;
}

标签:NSRange,OC,height,NSMutableString,range,str,使用,字符串
From: https://blog.51cto.com/u_15907570/5925303

相关文章

  • OC之【NSArray使用】
    #import<Foundation/Foundation.h>#import"Student.h"创建一个数组void//创建一个空的数组NSArray*array=[NSArrayarray];//创建有1个元素的数组NSAr......
  • OC之【NSMutableArray的使用】
    #import<Foundation/Foundation.h>#import"Student.h"voidNSMutableArray*array=[NSMutableArrayarrayWithObject:@"1"];//添加元素addObject:@"2"];addObj......
  • OC之【@protocol协议】
    #import<Foundation/Foundation.h>@protocolStudy<NSObject>默认就是@required-(void)test3;表示必须实现的方法//虽然字面上说是必须实现,但是编译器并不强求某个类......
  • OC之【@class】
    如果是继承某个类,就要导入类的头文件@classbook.h文件:#import<Foundation/Foundation.h>Book:NSObjectintprice;@end文件:"Book.h"Book-(void)dealloc{NSL......
  • OC之【深拷贝(mutableCopy)和浅拷贝(copy)】
    文件#import<Foundation/Foundation.h>#import"Student.h"#import"GoodStudent.h"//copy语法的目的:改变副本的时候,不会影响到源对象字符串的拷贝(深拷贝)//深拷贝:内容......
  • OC之【NSNumber的使用】
    #import<Foundation/Foundation.h>void//将int类型的10包装成一个NSNumber对象NSNumber*number=[NSNumbernumberWithInt:10];NSLog(@"number=%@",number);......
  • OC之【NSDictionary详解】
    main.m文件#import<Foundation/Foundation.h>#import"Student.h"字典的初始化void//NSDictionary是不可变的NSDictionary*dict=[NSDictionarydictionaryWit......
  • OC之【c语言结构体】
    <stdio.h>void//这个机构只能在函数内部使用//定义一个名为Student的结构体类型structintage;//年龄char*name;//姓名floatheight;//身高};......
  • OC之【NSMutableDictionary的使用】
    main.m文件#import<Foundation/Foundation.h>#import"Student.h"可变字典的使用void//创建一个空的字典NSMutableDictionary*dict=[NSMutableDictionarydicti......
  • OC之【NSValue的使用】
    #import<Foundation/Foundation.h>voidCGPointpoint=CGPointMake(10,10);//将结构体变量包装成一个对象NSValue*value=[NSValuevalueWithPoint:point];......