NSString NSArray和可变字符串、数组的用法

//
使用实例方法创建NSString对象

NSString *string1
=
[[NSStringalloc]initWithFormat:@"姓名流年"];

NSLog(@"%@", string1);

NSString *string2
=
[[NSStringalloc]initWithFormat:@"Ming"];

NSLog(@"%@", string2);

NSString *string3
=
[[NSStringalloc]initWithFormat:@"姓名:%@ 
年龄:%d 
爱好:%@",
@"嘿嘿",
100,
@"吃、喝、玩"
];

NSLog(@"%@",string3);

//把字符串转为数值类型

NSInteger intstr =
[@"124" integerValue];

NSLog(@"%ld",intstr);

double m =
[@"1000.00432" doubleValue];

NSLog(@"%lf", m);

//大小写转换

NSString *m1 = @"Have a good
day";

NSString *m2 = [m1
uppercaseString];

NSString *m3 = [m1
lowercaseString];

NSString *m4 = [m1
capitalizedString];

NSLog(@"===%@  ++++%@ 
****%@", m2, m3, m4);

//可变字符串

NSMutableString *string1
= [[NSMutableString
alloc]initWithFormat:@"蓝欧科技有限公司"];

NSMutableString *string2
= [NSMutableString
stringWithFormat:@"蓝欧科技有限公司"];

//拼接字符串

[string1
appendString:@".txt"];

NSLog(@"%@", string1);

//插入字符串

[string2
insertString:@"3G" atIndex:2];

NSLog(@"%@", string1);

//删除字符串

[string1
deleteCharactersInRange:NSMakeRange(2,
2)];

NSLog(@"%@", string1);

NSString *p0 =
@"aBcD_EfGk";

if([p0
hasSuffix:@"EfGk"])

{NSString *p1 =
[p0
stringByReplacingOccurrencesOfString:@"EfGk"withString:@"WXYZ"];

NSString *p2 = [p1
lowercaseString];

NSLog(@"%@",p2);

}

//使用类方法创建NSString对象

NSString *string2
=
[NSStringstringWithFormat:@"蓝欧科技有限公司"];

NSLog(@"%@", string2);

//直接赋值

NSString *string3 =
@"abc";

NSLog(@"%@", string3);

//获取字符串长度

NSInteger string1Length
= [string1 length];

NSLog(@"%ld", string1Length);

NSInteger len2 =
[string2 length];

NSLog(@"%ld", len2);

//判断字符串前缀

BOOL result1= [string1
hasPrefix:@"m"];

NSLog(@"------%d", result1);

BOOL result2 = [string1
hasSuffix:@"年"];

NSLog(@"%d",result2);

//查找字符串位置

NSRange range1 =
[string2
rangeOfString:@"有"];

NSLog(@"位置:%ld 
长度:%ld", range1.location,
range1.length);

NSRange kk = [string2
rangeOfString:@"科技有"];

NSLog(@"weizhi:%ld 
lengt:%ld", kk.location,
kk.length);

//截取字符串

NSString *p =
[[string2
substringFromIndex:2]substringToIndex:4];

NSString *p1 = [string2
substringToIndex:4];

NSLog(@"~~~~~~~%@,
%@", p, p1);

//NSArray数组类

//1.使用实例方法创建数组

NSArray *array1 =
[[NSArrayalloc]initWithObjects:@"1",@"2",@"3",
nil];

NSLog(@"%@", array1);

//2.使用类方法创建数组

NSArray *array2 =
[NSArrayarrayWithObjects:@"4",@"5",@"6",
nil];

NSLog(@"%@", array2);

//3.获取数组元素个数

NSUInteger count =
[array1 count];

NSLog(@"%ld", count);

//4.根据索引值获取对象

NSString *p = [array1
objectAtIndex:1];

NSLog(@"%@", p);

//5.获取对象在数组中的索引值

NSUInteger index =
[array1 indexOfObject:@"3"];

NSLog(@"%ld", index);

//NSMutableArray数组类

//1.使用实例方法创建

NSMutableArray *m1
=
[[NSMutableArrayalloc]initWithObjects:@"a",@"b",@"c",
nil];

//2。使用类方法创建

NSMutableArray *m2
=
[NSMutableArrayarrayWithObjects:@"a",@"b",@"c",
nil];

//3.添加元素

[m1
addObject:@"d"];

NSLog(@"%@",m1);

//4.插入元素

[m1
insertObject:@"ZZ"atIndex:1];

NSLog(@"%@",m1);

//5.删除元素

[m1
removeObject:@"ZZ"];

NSLog(@"%@",m1);

//6.替换元素

[m1
replaceObjectAtIndex:2withObject:@"Ming"];

NSLog(@"%@", m1);

//7.交换指定位置的2个元素

[m1
exchangeObjectAtIndex:1withObjectAtIndex:2];

NSLog(@"%@", m1);

//8.根据对象来交换两个元素的位置

[m1
exchangeObjectAtIndex:[m1
indexOfObject:@"a"]
withObjectAtIndex:[m1
indexOfObject:@"Ming"]];

NSLog(@"%@",m1);

//9.遍历数组对象
(循环输出)

for (int i
= 0; i < [m1 count]; i ++) {

NSString
*m2 = [m1 objectAtIndex:i];

NSLog(@"普通遍历:%@",m2);

}

for
(NSString *str  in m1) {

NSLog(@"快速遍历:%@",str);

}

NSMutableArray
*book =
[NSMutableArrayarrayWithObjects:@"AAA",@"BBB",@"CCC",@"DDD",
nil];

[book
addObject:@"MMM"];

[book
removeObject:@"CCC"];

[book
replaceObjectAtIndex:0withObject:@"WWW"];

NSUInteger i =
[book
indexOfObject:@"BBB"];

NSString *p1 = [book
objectAtIndex:i];

NSLog(@"%@", p1);

for
(NSString *list  in book) {

NSLog(@"%@",list);

}

NSLog(@"%@", book);

NSMutableArray *n
=
[NSMutableArrayarrayWithObjects:@"1",@"2",@"3",
nil];

NSNumber *num =
[NSNumbernumberWithFloat:4.4];

NSLog(@"%@",num);

[n addObject:num];

NSLog(@"%@",n);

NSNumber *n1 = [n
objectAtIndex:3];

NSLog(@"%@",n1);

CGFloat n2 = [n1
intValue];

NSLog(@"%.2lf",n2);

NSRange p2 =
NSMakeRange(4, 4);

NSString *p3 =
[string2
substringWithRange:NSMakeRange(2,4)];

NSLog(@"%@", p3);

//拼接

NSString *p4 =
[string2
stringByAppendingString:@"123"];

NSLog(@"%@",p4);

//替换

NSString *p5 =
[string2
stringByReplacingOccurrencesOfString:@"蓝欧"withString:@"abcdef"];

NSLog(@"%@", p5);

//比较

NSString *p6 =
@"a";

NSString *p7 =
@"B";

NSComparisonResult
result3 = [p6 compare:p7];

NSLog(@"%ld",result3);

NSString NSArray和可变字符串、数组的用法,布布扣,bubuko.com

时间: 2024-08-26 15:57:06

NSString NSArray和可变字符串、数组的用法的相关文章

NSString的形式--可变字符串--改方法Alter

前面我们讲解了可变形字符串的前三种方法, 现在我们来讲最后的一种改方法, 改方法和覆盖的方法有一些类似, 但又比覆盖的方法稍微不同, 它不是直接通过另外定义来进行覆盖的, 是可以通过位置和长度对原来的字符串进行修改覆盖. 下面涉及的方法有: replaceCharactersInRange: 它的意思就是通过NSMakeRange返回的位置和长度, 对原有的字符串进行修改. 下面是方法使用的简单例子: #import <Foundation/Foundation.h> int main(int

NSString的形式--可变字符串--减方法Delete

前面我们介绍过增方法和查方法, 现在我们来介绍减方法, 顾名思义, 减方法的意思肯定就是从原来的字符串中减掉一部分, 然后形成一个新的字符串. 这里涉及的方法: deleteCharactersInRange: 通过NSMakeRange返回的位置和长度告诉deleteCharacterInRange要从第几个开始删除, 删除的长度是多少. 下面是简单的使用例子: #import <Foundation/Foundation.h> int main(int argc, const char *

不可变字符串NSString

/*字符串的常用方法*/ //1.通常用来把一些基本数据类型和字符串进行拼接 int a = 9527; float b = 9527.0; NSString *string = [NSString stringWithFormat:@"%d%.1f",a,b]; NSLog(@"%@",string);//95279527.0 //2.字符串的长度(空格也会计算在内) NSString *string = @"今晚打老虎"; //NSLog(@

OC:可变字符串NSMutableString 的一些用法

NSString 是不可变字符串,所以方法调用时并不是对原字符串进行修改,而是先建立一个原字符串的副本,然后对副本内容进行修改. NSMutableString是NSString的子类,可以使用它的所有方法. 1.NSMutableString提供了附加字符串的方法. 可以使用appendString或appendFormat来对可变字符串操作: - (void) appendString: (NSString *) string; - (void) appendFormat: (NSStrin

可变字符串 插入 删除 重赋值 替换 前后缀 长度

#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { //nsstring父类   NSMutableString可变字符串子类 NSMutableString *mustr=[[NSMutableString alloc]init]; NSMutableString *mustr1=[NSMutableString stringWithFormat:@&qu

【转】不可变数组NSArray与可变数组NSMutableArray

原文网址:http://www.jianshu.com/p/1ad327f56d1d 不可变数组NSArray //创建一个空数组 NSArray *array = [NSArray array]; //这样创建是没有意义的. //创建只有一个元素的数组 NSArray *array1 = [NSArray arrayWithArray:@"a"]; NSArray *array2 = [NSArray arrayWithObject:@"b"]; //使用便利构造

Android中资源文件中的字符串数组string-array简单用法

在Android中,用string-array是一种简单的提取XML资源文件数据的方法. 例子如下: 把相应的数据放到values文件夹的strings.xml文件里,或是其他自定义的xml中都可以,以下操作方法相同. <?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="sports"> <item>足球<

不可变字符串 NSString与可变字符串 NSMutableString

不可变字符串 //创建一个字符串对象 NSString *string = [[NSString alloc] initWithFormat:@"zhong huang -"]; NSString *string1 = [[NSString alloc] initWithFormat:@"zhonger's  age is %d", 33]; NSString *string2 = [[NSString alloc] initWithFormat:@"zh

字符串数组和字典

[字段的权限] [注]在类中声明字段,可以为字段添加权限 @private 私有权限 @protected 保护权限 @public 公有权限 @interface Father : NSObject { @private    //私有权限修饰的变量,不能被子类继承,不能被外部函数访问.     int a; @protected  //缺省权限 保护权限修饰的变量,可以被子类继承,不能被外部函数访问.     int b;     int c; @public     //公有权限修饰的变量