首页 > 代码库 > 继续NSString的学习
继续NSString的学习
一、大小写转换
1、- (NSString *)uppercaseString; 所有字符转化为大写
2、- (NSString *)lowercaseString; 所有字符转化为小写
3、- (NSString *)capitalizedString; 所有单词首字母转化大写
NSLog(@"%@", [demoTwo uppercaseString]);
NSLog(@"%@", [demoTwo lowercaseString]);
NSLog(@"%@", [demoTwo capitalizedString]);
GOOD MORNING
good morning
Good Morning
五、- (NSRange)lineRangeForRange:(NSRange)range;返回字符串指定段的位置和长度
NSLog(@"%ld", [demoTwo lineRangeForRange:NSMakeRange(7, 2)].length);
12
六、- (void)getParagraphStart:(NSUInteger *)startPtr end:(NSUInteger *)parEndPtr contentsEnd:(NSUInteger *)contentsEndPtr forRange:(NSRange)range; 指定段分段取字符串
NSUInteger paraStart, paraEnd, contEnd;
NSString *aString1 = @"Apple\u2028Orange\u2029Banana\r\nLemon";
[aString1 getParagraphStart:¶Start end:¶End contentsEnd:&contEnd forRange:NSMakeRange(14, 1)] ;
NSLog(@"%@", [NSString stringWithFormat:@"ParagraphStart=%d, ParagraphEnd=%d, ContentsEnd=%d", paraStart, paraEnd, contEnd]);
八、- (NSString *)description; 返回字符串
NSLog(@"%@", [demoTwo description]);
good morning
十、编码处理
- (NSStringEncoding)fastestEncoding; // 字符串最快编码值
- (NSStringEncoding)smallestEncoding; // 字符串最小编码值
- (NSData *)dataUsingEncoding:(NSStringEncoding)encoding allowLossyConversion:(BOOL)lossy; // 返回指定编码的NSData对象,可以选择无损转化
- (NSData *)dataUsingEncoding:(NSStringEncoding)encoding; // 返回指定编码的NSData对象
- (BOOL)canBeConvertedToEncoding:(NSStringEncoding)encoding; //判断是否可以无损转化编码
NSLog(@"%lu", [demoTwo fastestEncoding]);
NSLog(@"%lu", [demo smallestEncoding]);
NSLog(@"%@", [demoTwo dataUsingEncoding:NSShiftJISStringEncoding allowLossyConversion:YES]);
NSLog(@"%@", [demoTwo dataUsingEncoding:NSWindowsCP1251StringEncoding]);
NSLog(@"%d", [demoTwo canBeConvertedToEncoding:NSMacOSRomanStringEncoding]);
30
30
<676f6f64 206d6f72 6e696e67>
<676f6f64 206d6f72 6e696e67>
1