NSString的使用

本文深入探讨了Objective-C与Objective-C++的关键操作与技术,包括字符串拼接、字符转整数、整数转字符、字符转浮点数、浮点数转字符等基本转换,以及如何在Objective-C中使用数组进行数据存储、遍历和管理,如创建数组、获取数组长度、通过索引获取对象、高速枚举、对象追加与删除等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

NSString *tempA = @"123";

  NSString *tempB = @"456";


1,字符串拼接

 NSString *newString = [NSString stringWithFormat:@"%@%@",tempA,tempB];


2,字符转int

int intString = [newString intValue];


3,int转字符

NSString *stringInt = [NSString stringWithFormat:@"%d",intString];


4,字符转float

 float floatString = [newString floatValue];


5,float转字符

NSString *stringFloat = [NSString stringWithFormat:@"%f",intString];


// insert code here...
NSLog(@"数组");
//指定多个字符串创建数组
NSArray *array;
array=[NSArray  arrayWithObjects:@"0-asd",@"1-fds",@"2-哈咯",@"3-个人",nil];
//数组的长度
NSLog(@"数组长度%d",array.count);
//通过索引取得对象
for(int i=0;i<array.count;i++)
{
    NSString *secondStr=[array objectAtIndex:i];
    NSLog(secondStr,nil);
}
//高速枚举法取得对象,Objective-C2.0开始支持,
for(NSString *str in array)
{
    NSLog(str,nil);
}
//对象的追加于删除
//创建空数组
NSMutableArray *MutArray=[NSMutableArray array];
//追加对象
[MutArray addObject:@"A"];
[MutArray addObjectsFromArray:array];
//插入对象
NSString *thstr=@"插入值";
[MutArray insertObject:thstr atIndex:4];
//替换对象
[MutArray replaceObjectAtIndex:2 withObject:@"替换"];
//删除所有对象
//[Mutarray removeAllObjects];
//删除最后的对象
[MutArray removeLastObject];
//删除索引为Index的对象
[MutArray removeObjectAtIndex:0];
//删除所有于object同值的对象
[MutArray removeObject:@"0-asd"];
//删除数组中所有与object等价的对象
[MutArray removeObjectIdenticalTo:thstr];
//删除数组中所有与数组array包含相同的元素
[MutArray removeObjectsInArray:array];
NSLog(@"%@",MutArray);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值