NSStirng和NSNumber随学随记

1.NSNumber

NSNumber可以将基本数据类型包装成对象。

+ (NSNumber *)numberWithInt:(int)value;
+ (NSNumber *)numberWithDouble:(double)value;
+ (NSNumber *)numberWithFloat:(Float)value;
+ (NSNumber *)numberWithBool:(BOOL)value;
+ (NSNumber *)numberWithChar:(Char)value;
...
还原基本数据类型
-(char) charValue;
-(int) intValue;
-(float) floatValue;
-(BOOL) boolValue;
-(NSString*) stringValue;

2.NSString

1.创建一个字符串常量

NSString *string = @"字符串常量";

2.创建一个空字符串

NSString *string = [[NSString alloc] init];

NSString *string = [NSString string];

3.快速创建一个字符串

NSString *string = [[NSString alloc] initWithString:@"This is a String!"];

NSString *string = [[NSString  stringWithString:@"This is a String!"];

4.格式化占位符

int i = 5;

NSString *string = [NSString stringWithFormat:@"This is %d",i];

5.比较字符串

//isEqualToString方法  
比较2个字符串内容是否相同

NSString *string1 = @"This is a String!";
NSString *string2 = @"This is a String!";
BOOL result = [string1 isEqualToString:astring2];

NSLog(@"result:%d",result);

//==方法

比较2个字符串是否相同对象(指针是否相同)

//compare方法

比较字符串大小,返回一个枚举类型NSOrderedSame,NSOrderedAscending = -1,NSOrderedDescending

-(NSComparisonResult) compare: (NSString *)string options:(unsigned)mask;

options 可使用位 or 或 运算符 | 添加选项标记

NSCaseInsensitiveSearch:不区分大小写

NSLiteralSearch:完全比较,区分大小写

NSNumericSearch:比较字符串个数,而不是数值

6.改变字符串大小写

NSString *string1 = @"A String";
NSString *string2 = @"String";

NSLog(@"string1:%@",[string1 uppercaseString]);//uppercaseString返回转换为大写的字符串
NSLog(@"string2:%@",[string2 lowercaseString]);//lowercaseString返回转换为小写的字符串
NSLog(@"string2:%@",[string2 capitalizedString]);//capitalizedString返回每个单词首字母大写的字符串

7.字符串转换成数组

NSString *string = [[NSString alloc] initWithString:@"One,Two,Three,Four"];

NSArray*array = [string componentsSeparatedByString:@","];

数组合成字符串

NSArray *array = [[NSArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four",nil];

NSString *string = [array componentsJoinedByString:@","];

8.截取字符串

-substringToIndex: 从字符串的开头一直截取到指定的位置,但不包括该位置的字符

-substringFromIndex: 以指定位置开始(包括指定位置的字符),并包括之后的全部字符

-substringWithRange: //按照所给出的位置,长度,任意地从字符串中截取子串

9.查找与替换

- (NSString *)stringByReplacingCharactersInRange:(NSRange)range withString:(NSString *)replacement
- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement

----查找-----
NSRange subRange = [String1 rangeOfString:@"is a"]; // 如果没查找到,则 (subRange.location == NSNotFound)为真。

10.  拼接字符串

-(NSString*)stringByAppendingString:(NSString *)string;

-(NSString*)stringByAppendingFormat:(NSString *)format,..;

NSMutableString(可修改的字符串)

+ (id)string;
- (void)appendString:(NSString *)string;
- (void)appendFormat:(NSString *)format, ...;

----- 在已有字符串中按照所给出范围删除字符----
//deleteCharactersInRange:
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 deleteCharactersInRange:NSMakeRange(0, 5)]; // 删除指定范围(location=0,length=5)的字符串
NSLog(@"String1:%@",String1);
----在已有字符串后面在所指定的位置中插入给出的字符串-----
//-insertString: atIndex:
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 insertString:@"Hi! " atIndex:0];
NSLog(@"String1:%@",String1);
[String1 insertString:@"and StringEnd", atIndex:[String1 length]]; // 在可变字符串的最后插入
----将已有的空符串换成其它的字符串-----
//-setString:
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 setString:@"Hello Word!"];
NSLog(@"String1:%@",String1);

----按照所给出的范围替换的原有的字符-----
//-setString:
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 replaceCharactersInRange:NSMakeRange(0, 4) withString:@"That"]; // 用于NSMutableString
NSLog(@"String1:%@",String1);
----在给定的范围内查找并替换-----
- (NSUInteger)replaceOccurrencesOfString:(NSString *)target withString:(NSString *)replacement options:(NSStringCompareOptions)opts range:(NSRange)searchRange

----判断字符串内是否还包含别的字符串(前缀,后缀)-----
//01: 检查字符串是否以另一个字符串开头- (BOOL) hasPrefix: (NSString *) aString;
NSString *String1 = @"NSStringInformation.txt";
[String1 hasPrefix:@"NSString"] = = 1 ? NSLog(@"YES") : NSLog(@"NO");
[String1 hasSuffix:@".txt"] = = 1 ? NSLog(@"YES") : NSLog(@"NO");

//02: 查找字符串某处是否包含给定的字符串 - (NSRange) rangeOfString: (NSString *) aString,这一点前面在串中搜索子串用到过

NSRange subRange;
subRange = [string1 rangeOfString:@"string A"]; //查找字符串string1中是否包含“string A”。返回NSRange类型。
if(subRange.location == NSNotFound)
NSLog(@"String not found ");
else NSLog(@"string is at index %lu, length is %lu", subRange.location, subRange.length);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值