NSString - 属性及方法归类
0. 实际使用
NSString —> 不可变字符串
- 创建方式: - NSString *s1 = @“jack” ; - NSString *s2 = [[NSString alloc] initWithString :@”jack”]; - NSString *s3 = [[NSString alloc] initWithFormat :@“age is %d” , 10]; - NSString *s4 = [[NSString alloc] initWithUTF8String: “jack”]; //C字符串 —>> OC字符串 - const *cs = [s4 UTF8String]; //OC字符串 —>> C字符串 - NSString *s5 = [[NSString alloc] initWithContentsOfFile:@“/Users/apple/Desktop/1.txt” encoding: NSUTF8StringEncoding error:nil ] ; - //URL :资源路径 - 协议头 : //路径 - file :// 本地文件 - ftp :// - http:// - NSURL *url = [[NSURL alloc] initWithString :@“file:///Users/apple/Desktop/1.txt”]; //对象方法的路径赋值 - NSURL *url2 = [NSURL fileURLWithPath:@“/Users/apple/Desktop/1.txt”]; //类方法的路径赋值 - 一般都会有一个类方法跟对象方法配对 - NSString *s6 = [[NSString alloc] initWithContenesOfURL: url encoding:NSUTF8StringEncoding error:nil]; - NSLog(@“s6= \n %@“, s6);
NSMutableString —> 可变字符串 ( 继承自 NSString )
- 可以像数组和字典一样, 拼接内容到原有字符串后面 - 例子: - NSMutableString *s1 = [NSMutableString stringWithFormat:@“my age is 10”]; - [s1 appendString:@“ 11 12”]; //拼接这些内容到s1的后面 - 可以获取一部分字符串并进行修改 - 例子: - NSRange range1 = [s1 rangeOfString:”is”]; - [s1 deleteCharactersInRange: range1]; //这两句通常都是一起出现的. 目的是无论要修改的目标是否被修改, 都能确保is会被删除 - NSString *s2 = [NSString stringWithFormat:@“age is 10”]; //这句的打印结果, 和s1最初赋值时一样. 但不能做改动 - NSString *s3 = [s2 stringByAppendingString:@“ 11 12”]; //这句的意思是, 新建一个字符串s3, 内容是 s2 +" 11 12"
1. NSStringCompare枚举
- NSCaseInsensitiveSearch 不区分大小写比较
- NSLiteralSearch 区分大小写比较
- NSBackwardsSearch 从字符串末尾开始搜索
- NSAnchoredSearch 搜索限制范围的字符串
- NSNumericSearch 按照字符串里的数字为依据,算出顺序。例如 Foo2.txt < Foo7.txt < Foo25.txt
- NSDiacriticInsensitiveSearch 忽略 “-” 符号的比较
- NSWidthInsensitiveSearch 忽略字符串的长度,比较出结果
- NSForcedOrderingSearch 忽略不区分大小写比较的选项,并强制返回 NSOrderedAscending 或者 NSOrderedDescending
- NSRegularExpressionSearch 只能应用于 rangeOfString:…, stringByReplacingOccurrencesOfString:…和 replaceOccurrencesOfString:… 方法。使用通用兼容的比较方法,如果设置此项,可以去掉 NSCaseInsensitiveSearch 和 NSAnchoredSearch
2. 属性:
@property (readonly) NSUInteger length; 长度
@property (readonly) double doubleValue; 转成double类型
@property (readonly) float floatValue; 转成float类型
@property (readonly) int intValue; 转成int类型
@property (readonly) NSInteger integerValue NS_AVAILABLE(10_5, 2_0); 转成integer类型
@property (readonly) long long longLongValue NS_AVAILABLE(10_5, 2_0); 转成longlong类型
@property (readonly) BOOL boolValue NS_AVAILABLE(10_5, 2_0); 转成bool类型
@property (readonly, copy) NSString *uppercaseString; 转成大写的字符串 => non-localized
@property (readonly, copy) NSString *lowercaseString; 转成小写的字符串 => non-localized
@property (readonly, copy) NSString *capitalizedString; 转成每个单词首字母大写,其他字母小写 => non-localized
@property (readonly, copy) NSString *localizedUppercaseString NS_AVAILABLE(10_11, 9_0); 转成大写的字符串 => localized-aware
@property (readonly, copy) NSString *localizedLowercaseString NS_AVAILABLE(10_11, 9_0); 转成小写的字符串 => localized-aware
@property (readonly, copy) NSString *localizedCapitalizedString NS_AVAILABLE(10_11, 9_0); 转成每个单词首字母大写,其他字母小写 => localized-aware
@property (readonly) NSStringEncoding fastestEncoding; // Result in 0(1) time; a rough estimate => 粗略估计, 结果在0~1内
@property (readonly) NSStringEncoding smallestEncoding; // Result in O(n) time; the encoding in which the string is most compact
@property (readonly, copy) NSString decomposedStringWithCanonicalMapping; 使用映射获取多个字符串 => 具体没用到
@property (readonly, copy) NSString *precomposedStringWithCanonicalMapping; 同上
@property (readonly, copy) NSString *decomposedStringWithCompatibilityMapping; 同上
@property (readonly, copy) NSString *precomposedStringWithCompatibilityMapping; 同上
@property (readonly, copy) NSString *description; 修改NSLog的默认输出
@property (readonly) NSUInteger hash; 用来加密( 主要给网络GET方法加密 )
3. 创建和初始化字符串
Creating and Initializing Strings
* + string
* – init
* – initWithBytes:length:encoding:
* – initWithBytesNoCopy:length:encoding:freeWhenDone:
* – initWithCharacters:length:
* – initWithCharactersNoCopy:length:freeWhenDone:
* – initWithString:
* – initWithCString:encoding:
* – initWithUTF8String:
* – initWithFormat:
* – initWithFormat:arguments:
* – initWithFormat:locale:
* – initWithFormat:locale:arguments:
* – initWithData:encoding:
* + stringWithFormat:
* + localizedStringWithFormat:
* + stringWithCharacters:length:
* + stringWithString:
* + stringWithCString:encoding:
* + stringWithUTF8String:
* + stringWithCString: Deprecated in iOS 2.0
* + stringWithCString:length: Deprecated in iOS 2.0
* – initWithCString: Deprecated in iOS 2.0
* – initWithCString:length: Deprecated in iOS 2.0
* – initWithCStringNoCopy:length:freeWhenDone: Deprecated in iOS 2.0
4. 从一个文件创建和初始化字符串
Creating and Initializing a String from a File
* + stringWithContentsOfFile:encoding:error:
* – initWithContentsOfFile:encoding:error:
* + stringWithContentsOfFile:usedEncoding:error:
* – initWithContentsOfFile:usedEncoding:error:
* + stringWithContentsOfFile: Deprecated in iOS 2.0
* – initWithContentsOfFile: Deprecated in iOS 2.0
5. 从一个 URL 创建和初始化字符串
Creating and Initializing a String from an URL
* + stringWithContentsOfURL:encoding:error:
* – initWithContentsOfURL:encoding:error:
* + stringWithContentsOfURL:usedEncoding:error:
* – initWithContentsOfURL:usedEncoding:error:
* + stringWithContentsOfURL: Deprecated in iOS 2.0
* – initWithContentsOfURL: Deprecated in iOS 2.0
6. 将字符串内容写到文件或 URL
Writing to a File or URL
* – writeToFile:atomically:encoding:error:
* – writeToURL:atomically:encoding:error:
* – writeToFile:atomically: Deprecated in iOS 2.0
* – writeToURL:atomically: Deprecated in iOS 2.0
7. 获取字符串长度
Getting a String’s Length
* – length
* – lengthOfBytesUsingEncoding:
* – maximumLengthOfBytesUsingEncoding:
8. 获取字符和字节
Getting Characters and Bytes
* – characterAtIndex:
* – getCharacters:range:
* – getBytes:maxLength:usedLength:encoding:options:range:remainingRange:
* – getCharacters: Deprecated in iOS 4.0
9. 获取 C 字符串
Getting C Strings
* – cStringUsingEncoding:
* – getCString:maxLength:encoding:
* – UTF8String
* – cString Deprecated in iOS 2.0
* – cStringLength Deprecated in iOS 2.0
* – getCString: Deprecated in iOS 2.0
* – getCString:maxLength: Deprecated in iOS 2.0
* – getCString:maxLength:range:remainingRange: Deprecated in iOS 2.0
* – lossyCString Deprecated in iOS 2.0
10. 拼接字符串
Combining Strings
* – stringByAppendingFormat: 用指定字符进行拼接
* – stringByAppendingString: 用指定字符串进行拼接
* – stringByPaddingToLength:withString:startingAtIndex: 把指定字符串, 用指定下标的”填充字符串”进行填充, 直到指定字符串的总长度达到xx数量
11. 拆分字符串
Dividing Strings
* – componentsSeparatedByString: 用指定的string ,对目标字符串进行分割, 得到一个数组,里面是分割剩下的小段字符串
* – componentsSeparatedByCharactersInSet: 用指定的字符或标点符号(注意是无序的,按每个字符或符号为一个分割单位), 对目标字符串进行分割, 得到一个数组.
* – stringByTrimmingCharactersInSet: 能去掉字符串前后的空格. 更多功能没研究清楚
* – substringFromIndex: 从指定字符串的第x个下标取值到最后
* – substringWithRange: 从指定字符串中截取指定的range范围的字符串
* – substringToIndex: 从指定字符串的第0个下标取值到指定的下标数xx
12. 查找字符和子字符串
Finding Characters and Substrings
* – rangeOfCharacterFromSet:
* – rangeOfCharacterFromSet:options:
* – rangeOfCharacterFromSet:options:range:
* – rangeOfString: 查找指定字符串从一些字符串中, 获得第一个匹配的range
* – rangeOfString:options:
* – rangeOfString:options:range:
* – rangeOfString:options:range:locale:
* – enumerateLinesUsingBlock:
* – enumerateSubstringsInRange:options:usingBlock:
13. 替换字符串
Replacing Substrings
* – stringByReplacingOccurrencesOfString:withString: 用新字符串, 替换指定的字符串
* – stringByReplacingOccurrencesOfString:withString:options:range: 没用过.
* – stringByReplacingCharactersInRange:withString: 把新字符串, 替换到指定的range范围 ,=> NSRange range = {开始的字符串数, 替换范围的字符串总长度}
14. 确定行和段落的范围
Determining Line and Paragraph Ranges
* – getLineStart:end:contentsEnd:forRange:
* – lineRangeForRange:
* – getParagraphStart:end:contentsEnd:forRange:
* – paragraphRangeForRange:
15. 确定组成字符的顺序*
Determining Composed Character Sequences
* – rangeOfComposedCharacterSequenceAtIndex:
* – rangeOfComposedCharacterSequencesForRange:
16. 将字符串内容转换成属性列表
Converting String Contents Into a Property List
* – propertyList
* – propertyListFromStringsFileFormat
17. 识别和比较字符串
Identifying and Comparing Strings
* – caseInsensitiveCompare:
* – localizedCaseInsensitiveCompare:
* – compare:
* – localizedCompare:
* – compare:options:
* – compare:options:range:
* – compare:options:range:locale:
* – localizedStandardCompare:
* – hasPrefix:
* – hasSuffix:
* – isEqualToString:
* – hash
18. 拆叠字符串*
Folding Strings
* – stringByFoldingWithOptions:locale:
19. 获取公共的前缀
Getting a Shared Prefix
* – commonPrefixWithString:options:
20. 改变大小写
Changing Case
* – capitalizedString
* – capitalizedStringWithLocale:
* – lowercaseString
* – lowercaseStringWithLocale:
* – uppercaseString
* – uppercaseStringWithLocale:
21. 使用映射获取多个字符串*
Getting Strings with Mapping
* – decomposedStringWithCanonicalMapping
* – decomposedStringWithCompatibilityMapping
* – precomposedStringWithCanonicalMapping
* – precomposedStringWithCompatibilityMapping
22. 获取数字值
Getting Numeric Values
* – doubleValue
* – floatValue
* – intValue
* – integerValue
* – longLongValue
* – boolValue
23. 处理字符编码
Working with Encodings
* + availableStringEncodings
* + defaultCStringEncoding
* + localizedNameOfStringEncoding:
* – canBeConvertedToEncoding:
* – dataUsingEncoding:
* – dataUsingEncoding:allowLossyConversion:
* – description
* – fastestEncoding
* – smallestEncoding
24. 处理路径
Working with Paths
* + pathWithComponents:
* – pathComponents
* – completePathIntoString:caseSensitive:matchesIntoArray:filterTypes:
* – fileSystemRepresentation
* – getFileSystemRepresentation:maxLength:
* – isAbsolutePath
* – lastPathComponent
* – pathExtension
* – stringByAbbreviatingWithTildeInPath
* – stringByAppendingPathComponent:
* – stringByAppendingPathExtension:
* – stringByDeletingLastPathComponent
* – stringByDeletingPathExtension
* – stringByExpandingTildeInPath
* – stringByResolvingSymlinksInPath
* – stringByStandardizingPath
* – stringsByAppendingPaths:
25. 处理 URL
Working with URLs
* – stringByAddingPercentEscapesUsingEncoding:
* – stringByReplacingPercentEscapesUsingEncoding:
* – stringByAddingPercentEncodingWithAllowedCharacters:
* – stringByRemovingPercentEncoding
26. 语言标记和分析*
Linguistic Tagging and Analysis
* – enumerateLinguisticTagsInRange:scheme:options:orthography:usingBlock:
* – linguisticTagsInRange:scheme:options:orthography:tokenRanges: