//
使用实例方法创建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