字符串的使用
NSString的创建方式
指针指向的对象地址相同,指针的地址不同
第一种创建方式 常量区中的字符串只要内容一致, 不会重复创建
NSString *str1 = @"lion";
NSString *str2 = @"lion";
第二种创建方式,堆区中得字符串哪怕内容一致, 也会重复创建
NSString *str1 = [NSString stringWithFormat:@"lion"];
NSString *str2 = [NSString stringWithFormat:@"lion"];
指针常量 常量指针
指针常量,地址和值都不可更改
NSString *const str = @"xxxxx”;
常量指针,地址可更改
const NSString * str2 = @"明月几时有";
NSString const * str3 = @"唧唧复唧唧”;
字符串操作
字符串的比较
NSString的比较
- (BOOL)isEqualToString:(NSString *)aString;
地址是否一样
str1 == str2
比较字符串大小
- (NSComparisonResult)compare:(NSString *)string;
忽略大小写进行比较
- (NSComparisonResult)compare:(NSString *)string;
比较两个字符串大小
[deal.publish_date compare:now]==NSOrderedAscending;
字符串搜索
是否以aString开头
- (BOOL)hasPrefix:(NSString *)aString;
是否以aString结尾
- (BOOL)hasSuffix:(NSString *)aString;
用来检查字符串内容中是否包含了aString,如果包含, 就返回aString的范围,如果不包含, NSRange的location为NSNotFound, length为0
- (NSRange)rangeOfString:(NSString *)aString;
返回 index 对应的位置的字符
- (unichar)characterAtIndex:(NSUInteger)index;
字符串是否包含字符字符串
- (BOOL)containsString:(NSString *)str
** 字符串的位置**
NSInteger dotloc =[self.currentPriceLabel.text rangeOfString:@"."].location;
if (dotloc!=NSNotFound) {NSLog(@"没有找到");}
最后一个“<”符号的位置
NSRange lastRange = [source rangeOfString:@"<" options:NSBackwardsSearch];
字符串的截取
- (NSString *)substringFromIndex:(NSUInteger)from;
- (NSString *)substringToIndex:(NSUInteger)to;
- (NSString *)substringWithRange:(NSRange)range;
字符串的替换
- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement;
去除首尾
- (NSString *)stringByTrimmingCharactersInSet:(NSCharacterSet *)set;
去除首尾的空格
NSString *newStr = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
字符串的转换
转为基本数据类型
- (int)intValue;
转成对象类型
@() 快速把int转化为NSNumber对象类型 NSNumber
@[] 快速声明数组 NSArray
@{}字典类型 NSDiction
@YES @1 @1.1 @=(变量);
转为C语言中的字符串
- (char *)UTF8String;
const char *cStr = [str UTF8String];
NSString—>char *
`const char *c=[str cStringUsingEncoding:NSUTF8StringEncoding];`
转为对象类型
NSString *str = [NSString stringWithUTF8String:cStr];
NSString的大小写转换
全部字符转为大写字母
- (NSString *)uppercaseString;
全部字符转为小写字母
- (NSString *)lowercaseString
首字母变大写,其他字母都变小写
- (NSString *)capitalizedString
NSString—>NSData
NSData *data2=[str2 dataUsingEncoding:NSUTF8StringEncoding];
中文转码
可以解决url含有中文的问题
iOS9之前
`[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];`
iOS9之后
`[path stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLPathAllowedCharacterSet]]`
关于文档的操作
字符串与路径
获取到沙河路劲
NSString *path2 = NSHomeDirectory();
获取到library的路径
NSString *path = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
是否为绝对路劲其实就是判断是否以/开头
- (BOOL)isAbsolutePath;
- (NSString *)lastPathComponent;
- (NSString *)stringByDeletingLastPathComponent;
增
- (NSString *)stringByAppendingPathComponent:(NSString *)str;(也可以使用stringByAppendingString:或者stringByAppendingFormat:拼接字符串内容)
**NSString 与 文件拓展名**
- (void)pathExtension
- (NSString *)stringByDeletingPathExtension;
在尾部添加一个拓展名
- (NSString *)stringByAppendingPathExtension:(NSString *)str;
- (void)stringByAppendingPathExtension
将一个字符串分隔成多个字符串
- (NSArray *)componentsSeparatedByString:(NSString *)separator
NSString的读写
读取:错误error返回0
NSError *error = nil;
NSString *str = [NSString stringWithContentsOfFile:@"/Users/LJH/Desktop/Lion.txt" encoding:NSUTF8StringEncoding error:&error];`
写入:重复写入同一个文件会覆盖上一个文件
BOOL flag = [str writeToFile:@"/Users/LJH/Desktop/Lion.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSMutableString常用方法
增
[strM insertString:@"http://" atIndex:0]
[mstr appendFormat:@"你好%@",@"hare"];
[strM appendString:@"Lion"];
- (void)appendFormat:(NSString *)format;
NSMutableString *strM = [NSMutableString stringWithString:@"http://www.baidu.com"];
删
[path deleteCharactersInRange:range];`
改
- (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)aString;
NSRange range = [strM rangeOfString:@"lion"];
NSMutableAttributedString
3.1 创建
NSMutableAttributedString *attributedTitleText = [[NSMutableAttributedString alloc]initWithString:titleText];
根据附件创建一个属性文字
NSAttributedString *imageStr = [NSAttributedString attributedStringWithAttachment:attch];
** 属性**
属性的文字
self.attributedText
.string
** 方法**
添加属性:根据key-value
- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;
// 添加属性:根据字典
- (void)addAttributes:(NSDictionary<NSString *, id> *)attrs range:(NSRange)range;
// 设置range范围的属性,会覆盖之前的
- (void)setAttributes:(nullable NSDictionary<NSString *, id> *)attrs range:(NSRange)range;
根据一个key读取一个对象
StatusAttributedTextRangesModel *textRangesModel = [self.attributedText attribute:StatusAttributedTextRangesModelKey atIndex:0 effectiveRange:NULL];
截取
[self.attributedText attributedSubstringFromRange:range]
获取改属性对应的对象
[text attribute:YYTextBindingAttributeName atIndex:range.location effectiveRange:NULL]
遍历
[self.attributedText enumerateAttributesInRange:NSMakeRange(0, self.attributedText.length) options:NSAttributedStringEnumerationReverse usingBlock:^(NSDictionary *attrs, NSRange range, BOOL *stop) {
EmotionTextAttachment *attch = attrs[@"NSAttachment"];
}];
NSTextAttachment附件
** 创建**
NSTextAttachment *attch = [[NSTextAttachment alloc] init];
** 属性**
将一个图片,转成附件
attch.image = [UIImage imageNamed:emotion.png];
** 设置附件的bounds**
attch.bounds = CGRectMake(0, -4, attchWH, attchWH);
3.5 文字字典
背景颜色
NSBackgroundColorAttributeName
文字颜色
NSForegroundColorAttributeName
字体
NSFontAttributeName
是否带有下划线
attributes[NSUnderlineStyleAttributeName] = @YES;
文字颜色
normalAttrs[UITextAttributeTextColor]