zoukankan      html  css  js  c++  java
  • NSString

    一、NSString.h文件所在
        1、    IOS  
           $1  /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.1.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSString.h
           $2 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSString.h
        2、 mac 
          /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/Foundation.framework/Versions/C/Headers/NSString.h
    /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/System/Library/Frameworks/Foundation.framework/Versions/C/Headers/NSString.h

     一、NSString 介绍
    NSString其实是一个对象类型。NSString是NSObject(Cocoa Foundation的基础对象)的子类,不可改变字符串,NSMutableString为可变的字符串。
    NSString 与 char* 最大的区别就是 NSString是一个objective对象,而char* 是一个字节数组。@+" 字符串 " 这个符号为objective-c NSString 字符串常量的标准用法,char* 创建的时候 无需添加@。每一个字符串其实是由若干个char字符组成,字符串的遍历实际上就是将字符串中的每一个字符提取出来。
    NSString 提供2个消息,length和characterAtIndex
    NSString 核心基础接口为CFStringRef

    二、NSString 功能细分
    1、字符串的比较 
    2、字符串的截取和大小写
    3、搜索字符串与替换字符串
    4、字符串中删除元素,插入,追加
    5、字符串与指定类型转换
    6、字符串编码转换
    7、 文件字符串操作(读写)

    二 、NSString.h分析

     

    复制代码
    /* NSString.h
    Copyright (c) 1994-2012, Apple Inc. All rights reserved.
    */
    
    
    typedef unsigned short unichar;  短正值整型,不少于16位
    
    
    #import <limits.h> 
    #import <Foundation/NSObject.h> 根类,提供基本运行能力的接口
    #import <Foundation/NSRange.h>  截取字符串或者数组
    #import <stdarg.h>  C标准函数库的头文件,stdarg主要目的是为让函数能够接收不定参数
    
    
    @class NSData, NSArray, NSDictionary, NSCharacterSet, NSURL, NSError, NSLocale;   //@class  告诉编译器,其后面声明的名称是类的名称
    
    
    FOUNDATION_EXPORT NSString * const NSParseErrorException; // raised by -propertyList  异常处理处理串定义
    
    
    #define NSMaximumStringLength (INT_MAX-1)  //字符串最大值定义
    
    
    /* These options apply to the various search/find and comparison methods (except where noted). * 下面部分主要用于搜索,查找,比较
    */
    typedef NS_OPTIONS(NSUInteger, NSStringCompareOptions) {
        NSCaseInsensitiveSearch = 1, //不区分大小写比较
        NSLiteralSearch = 2, /* Exact character-by-character equivalence */ //区分大小写比较
        NSBackwardsSearch = 4, /* Search from end of source string */ //从字符串末尾开始搜索
        NSAnchoredSearch = 8, /* Search is limited to start (or end, if NSBackwardsSearch) of source string */ 搜索限制范围的字符串
        NSNumericSearch = 64, /* Added in 10.2; Numbers within strings are compared using numeric value, that is, Foo2.txt < Foo7.txt < Foo25.txt; only applies to compare methods, not find *//按照字符串里的数字为依据,算出顺序
        NSDiacriticInsensitiveSearch NS_ENUM_AVAILABLE(10_5, 2_0) = 128, /* If specified, ignores diacritics (o-umlaut == o) */ //忽略 "-" 符号的比较
        NSWidthInsensitiveSearch NS_ENUM_AVAILABLE(10_5, 2_0) = 256, /* If specified, ignores width differences ('a' == UFF41) *//忽略字符串的长度,比较出结果
        NSForcedOrderingSearch NS_ENUM_AVAILABLE(10_5, 2_0) = 512, /* If specified, comparisons are forced to return either NSOrderedAscending or NSOrderedDescending if the strings are equivalent but not strictly equal, for stability when sorting (e.g. "aaa" > "AAA" with NSCaseInsensitiveSearch specified) */ 忽略不区分大小写比较的选项,并强制返回 
        NSRegularExpressionSearch NS_ENUM_AVAILABLE(10_7, 3_2) = 1024    /* Applies to rangeOfString:..., stringByReplacingOccurrencesOfString:..., and replaceOccurrencesOfString:... methods only; the search string is treated as an ICU-compatible regular expression; if set, no other options can apply except NSCaseInsensitiveSearch and NSAnchoredSearch */
    };
    
    
    /* Note that in addition to the values explicitly listed below, NSStringEncoding supports encodings provided by CFString.
    See CFStringEncodingExt.h for a list of these encodings.
    See CFString.h for functions which convert between NSStringEncoding and CFStringEncoding.
    */
    enum {
        NSASCIIStringEncoding = 1, /* 0..127 only */
        NSNEXTSTEPStringEncoding = 2,
        NSJapaneseEUCStringEncoding = 3,
        NSUTF8StringEncoding = 4,
        NSISOLatin1StringEncoding = 5,
        NSSymbolStringEncoding = 6,
        NSNonLossyASCIIStringEncoding = 7,
        NSShiftJISStringEncoding = 8,          /* kCFStringEncodingDOSJapanese */
        NSISOLatin2StringEncoding = 9,
        NSUnicodeStringEncoding = 10,
        NSWindowsCP1251StringEncoding = 11,    /* Cyrillic; same as AdobeStandardCyrillic */
        NSWindowsCP1252StringEncoding = 12,    /* WinLatin1 */
        NSWindowsCP1253StringEncoding = 13,    /* Greek */
        NSWindowsCP1254StringEncoding = 14,    /* Turkish */
        NSWindowsCP1250StringEncoding = 15,    /* WinLatin2 */
        NSISO2022JPStringEncoding = 21,        /* ISO 2022 Japanese encoding for e-mail */
        NSMacOSRomanStringEncoding = 30,
    
    
        NSUTF16StringEncoding = NSUnicodeStringEncoding,      /* An alias for NSUnicodeStringEncoding */
    
    
        NSUTF16BigEndianStringEncoding = 0x90000100,          /* NSUTF16StringEncoding encoding with explicit endianness specified */
        NSUTF16LittleEndianStringEncoding = 0x94000100,       /* NSUTF16StringEncoding encoding with explicit endianness specified */
    
    
        NSUTF32StringEncoding = 0x8c000100,                   
        NSUTF32BigEndianStringEncoding = 0x98000100,          /* NSUTF32StringEncoding encoding with explicit endianness specified */
        NSUTF32LittleEndianStringEncoding = 0x9c000100        /* NSUTF32StringEncoding encoding with explicit endianness specified */
    };
    typedef NSUInteger NSStringEncoding;
    
    
    typedef NS_OPTIONS(NSUInteger, NSStringEncodingConversionOptions) {
        NSStringEncodingConversionAllowLossy = 1,
        NSStringEncodingConversionExternalRepresentation = 2
    };
    
    
    FOUNDATION_EXPORT NSString * const NSCharacterConversionException;
    
    
    
    
    @interface NSString : NSObject <NSCopying, NSMutableCopying, NSSecureCoding> 协议
    
    
    /* NSString primitive (funnel) methods. A minimal subclass of NSString just needs to implement these, although we also recommend getCharacters:range:. See below for the other methods.
    */
    - (NSUInteger)length;  返回字符串的长度
    - (unichar)characterAtIndex:(NSUInteger)index; 返回在字符串中的某个位置的字符
    
    
    @end
    
    
    @interface NSString (NSStringExtensionMethods) 类别
    
    
    - (void)getCharacters:(unichar *)buffer range:(NSRange)aRange; unichar是两字节长的char,代表unicode的一个字符,截取字符串指定段- (NSString *)substringFromIndex:(NSUInteger)from; 截取指定位置后面的字符串
    - (NSString *)substringToIndex:(NSUInteger)to; 截取指定长度的字符串,从索引0开始
    - (NSString *)substringWithRange:(NSRange)range;    // Hint: Use with rangeOfComposedCharacterSequencesForRange: to avoid breaking up composed characters 截取字符串指定段,返回类型NSString
    
    
    /* In the compare: methods, the range argument specifies the subrange, rather than the whole, of the receiver to use in the comparison. The range is not applied to the search string.  For example, [@"AB" compare:@"ABC" options:0 range:NSMakeRange(0,1)] compares "A" to "ABC", not "A" to "A", and will return NSOrderedAscending.
    */
    - (NSComparisonResult)compare:(NSString *)string; 比较字符串
    - (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask; 以某些条件比较字符串
    - (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange;  以某些条件比较字符串至指定段
    - (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange locale:(id)locale; // locale arg used to be a dictionary pre-Leopard. We now accept NSLocale. Assumes the current locale if non-nil and non-NSLocale. nil continues to mean canonical compare, which doesn't depend on user's locale choice. 以某些限制+本地化(语言环境)比较条件比较字符串至指定段
    - (NSComparisonResult)caseInsensitiveCompare:(NSString *)string;不区别大小比较
    - (NSComparisonResult)localizedCompare:(NSString *)string;本地化比较
    - (NSComparisonResult)localizedCaseInsensitiveCompare:(NSString *)string; 不区别大小本地化比较
    
    
    /* localizedStandardCompare:, added in 10.6, should be used whenever file names or other strings are presented in lists and tables where Finder-like sorting is appropriate.  The exact behavior of this method may be tweaked in future releases, and will be different under different localizations, so clients should not depend on the exact sorting order of the strings.
    */
    - (NSComparisonResult)localizedStandardCompare:(NSString *)string NS_AVAILABLE(10_6, 4_0);
    
    - (BOOL)isEqualToString:(NSString *)aString; 测试两个字符串是否相等
    - (BOOL)hasPrefix:(NSString *)aString;  测试字符串是否以 nsstring 开始
    - (BOOL)hasSuffix:(NSString *)aString; 测试字符串是否以 nsstring 结尾
    
    
    /* These methods return length==0 if the target string is not found. So, to check for containment: ([str rangeOfString:@"target"].length > 0).  Note that the length of the range returned by these methods might be different than the length of the target string, due composed characters and such.
    */
    typedef struct _NSRange {
        NSUInteger location;
        NSUInteger length;
    } NSRange;
    - (NSRange)rangeOfString:(NSString *)aString; 搜索字符串One是否存在于字符串Two
    - (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask; 以某些限制条件搜索字符串One是否存在于字符串Two
    - (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask range:(NSRange)searchRange; 以某些限制条件搜索字符串One是否存在于字符串Two指定位置段
    - (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask range:(NSRange)searchRange locale:(NSLocale *)locale NS_AVAILABLE(10_5, 2_0);以某些限制条件+本地化限制搜索字符串One是否存在于字符串Two指定位置段
    
    
    /* These return the range of the first character from the set in the string, not the range of a sequence of characters. 
    */
    - (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)aSet; 指定字符集搜索
    - (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)aSet options:(NSStringCompareOptions)mask;以某些限制条件指定字符集搜索
    - (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)aSet options:(NSStringCompareOptions)mask range:(NSRange)searchRange; 以某些限制条件指定字符集搜索指定位置段
    
    
    - (NSRange)rangeOfComposedCharacterSequenceAtIndex:(NSUInteger)index; 以字符串的字符编码指定索引查找位置
    - (NSRange)rangeOfComposedCharacterSequencesForRange:(NSRange)range NS_AVAILABLE(10_5, 2_0); 以字符串的字符编码指定区域段查找位置
    
    
    - (NSString *)stringByAppendingString:(NSString *)aString; 将字符串One添加字符串Two后面
    - (NSString *)stringByAppendingFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2); 将多个字符串添加字符串Two后面
    
    
    /* The following convenience methods all skip initial space characters (whitespaceSet) and ignore trailing characters. NSScanner can be used for more "exact" parsing of numbers.
    */
    - (double)doubleValue; 返回转化的double类型
    - (float)floatValue;  返回转化的float类型
    - (int)intValue;  返回转化的int类型
    - (NSInteger)integerValue NS_AVAILABLE(10_5, 2_0);  返回转化的NSInteger(32位系统NSInteger是一个int,即32位,但当时64位系统时,NSInteger便是64位的)类型
    - (long long)longLongValue NS_AVAILABLE(10_5, 2_0); 返回转化的 长int类型
    - (BOOL)boolValue NS_AVAILABLE(10_5, 2_0);  // Skips initial space characters (whitespaceSet), or optional -/+ sign followed by zeroes. Returns YES on encountering one of "Y", "y", "T", "t", or a digit 1-9. It ignores any trailing characters. 返回转化的 长BOOL类型
    
    
    - (NSArray *)componentsSeparatedByString:(NSString *)separator; 字符串转化为数组
    - (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator NS_AVAILABLE(10_5, 2_0);  依据字符编码,分割字符串
    
    
    - (NSString *)commonPrefixWithString:(NSString *)aString options:(NSStringCompareOptions)mask;
    
    
    /* The following three case methods perform the canonical (non-localized) mappings. They are suitable for programming operations that require stable results not depending on the user's locale preference.  For localized case mapping for strings presented to users, use their corresponding methods with locale argument below.
     */
    - (NSString *)uppercaseString; 所有字符转化为大写
    - (NSString *)lowercaseString; 所有字符转化为小写
    - (NSString *)capitalizedString; 所有单词首字母转化大写
    
    
    /* The following methods perform localized case mappings based on the locale specified. Passing nil indicates the canonical mapping.  For the user preference locale setting, specify +[NSLocale currentLocale].
     */
    - (NSString *)uppercaseStringWithLocale:(NSLocale *)locale NS_AVAILABLE(10_8, 6_0); 所有字符转化为大写(本地化)
    - (NSString *)lowercaseStringWithLocale:(NSLocale *)locale NS_AVAILABLE(10_8, 6_0);所有字符转化为小写(本地化)
    - (NSString *)capitalizedStringWithLocale:(NSLocale *)locale NS_AVAILABLE(10_8, 6_0); 所有单词首字母转化大写(本地化)
    
    
    - (NSString *)stringByTrimmingCharactersInSet:(NSCharacterSet *)set; 字符串替换
    - (NSString *)stringByPaddingToLength:(NSUInteger)newLength withString:(NSString *)padString startingAtIndex:(NSUInteger)padIndex;将字符串two指定索引段添加指定索引段字符串One
    
    - (void)getLineStart:(NSUInteger *)startPtr end:(NSUInteger *)lineEndPtr contentsEnd:(NSUInteger *)contentsEndPtr forRange:(NSRange)range; 指定段分行去字符串
    - (NSRange)lineRangeForRange:(NSRange)range; 返回字符串指定段的位置和长度
    
    
    - (void)getParagraphStart:(NSUInteger *)startPtr end:(NSUInteger *)parEndPtr contentsEnd:(NSUInteger *)contentsEndPtr forRange:(NSRange)range;指定段分段取字符串
    - (NSRange)paragraphRangeForRange:(NSRange)range; 指定段分段的位置和长度
    
    #if NS_BLOCKS_AVAILABLE
    typedef NS_OPTIONS(NSUInteger, NSStringEnumerationOptions) {
        // Pass in one of the "By" options:
        NSStringEnumerationByLines = 0,                       // Equivalent to lineRangeForRange:
        NSStringEnumerationByParagraphs = 1,                  // Equivalent to paragraphRangeForRange:
        NSStringEnumerationByComposedCharacterSequences = 2,  // Equivalent to rangeOfComposedCharacterSequencesForRange:
        NSStringEnumerationByWords = 3,
        NSStringEnumerationBySentences = 4,
        // ...and combine any of the desired additional options:
        NSStringEnumerationReverse = 1UL << 8,
        NSStringEnumerationSubstringNotRequired = 1UL << 9,
        NSStringEnumerationLocalized = 1UL << 10              // User's default locale
    };
    
    
    /* In the enumerate methods, the blocks will be invoked inside an autorelease pool, so any values assigned inside the block should be retained.
    */
    - (void)enumerateSubstringsInRange:(NSRange)range options:(NSStringEnumerationOptions)opts usingBlock:(void (^)(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop))block NS_AVAILABLE(10_6, 4_0); 检查是否在指定范围内是否有匹配的字符串
    - (void)enumerateLinesUsingBlock:(void (^)(NSString *line, BOOL *stop))block NS_AVAILABLE(10_6, 4_0); 枚举字符串所有行
    #endif
    
    
    - (NSString *)description;    返回字符串
    
    
    - (NSUInteger)hash; 返回字符串哈希地址
    
    
    /*** Encoding methods ***/
    
    
    - (NSStringEncoding)fastestEncoding;     // Result in O(1) time; a rough estimate  字符串最快编码值
    - (NSStringEncoding)smallestEncoding;    // Result in O(n) time; the encoding in which the string is most compact   字符串最小编码值
    
    
    - (NSData *)dataUsingEncoding:(NSStringEncoding)encoding allowLossyConversion:(BOOL)lossy;   // External representation 返回指定编码的NSData对象
    - (NSData *)dataUsingEncoding:(NSStringEncoding)encoding;                                    // External representation 返回指定编码的NSData对象
    
    
    - (BOOL)canBeConvertedToEncoding:(NSStringEncoding)encoding; 判断是否可以无损转化编码
    
    
    /* Methods to convert NSString to a NULL-terminated cString using the specified encoding. Note, these are the "new" cString methods, and are not deprecated like the older cString methods which do not take encoding arguments.
    */
    - (__strong const char *)cStringUsingEncoding:(NSStringEncoding)encoding NS_RETURNS_INNER_POINTER; // "Autoreleased"; NULL return if encoding conversion not possible; for performance reasons, lifetime of this should not be considered longer than the lifetime of the receiving string (if the receiver string is freed, this might go invalid then, before the end of the autorelease scope) char编码转换
    - (BOOL)getCString:(char *)buffer maxLength:(NSUInteger)maxBufferCount encoding:(NSStringEncoding)encoding; // NO return if conversion not possible due to encoding errors or too small of a buffer. The buffer should include room for maxBufferCount bytes; this number should accomodate the expected size of the return value plus the NULL termination character, which this method adds. (So note that the maxLength passed to this method is one more than the one you would have passed to the deprecated getCString:maxLength:.) c字符串编码转化是否成功
    
    
    /* Use this to convert string section at a time into a fixed-size buffer, without any allocations.  Does not NULL-terminate. 
        buffer is the buffer to write to; if NULL, this method can be used to computed size of needed buffer.
        maxBufferCount is the length of the buffer in bytes. It's a good idea to make sure this is at least enough to hold one character's worth of conversion. 
        usedBufferCount is the length of the buffer used up by the current conversion. Can be NULL.
        encoding is the encoding to convert to.
        options specifies the options to apply.
        range is the range to convert.
        leftOver is the remaining range. Can be NULL.
        YES return indicates some characters were converted. Conversion might usually stop when the buffer fills, 
          but it might also stop when the conversion isn't possible due to the chosen encoding. 
    */
    - (BOOL)getBytes:(void *)buffer maxLength:(NSUInteger)maxBufferCount usedLength:(NSUInteger *)usedBufferCount encoding:(NSStringEncoding)encoding options:(NSStringEncodingConversionOptions)options range:(NSRange)range remainingRange:(NSRangePointer)leftover; 指定缓存区转换
    
    
    /* These return the maximum and exact number of bytes needed to store the receiver in the specified encoding in non-external representation. The first one is O(1), while the second one is O(n). These do not include space for a terminating null.
    */
    - (NSUInteger)maximumLengthOfBytesUsingEncoding:(NSStringEncoding)enc; // Result in O(1) time; the estimate may be way over what's needed. Returns 0 on error (overflow) 字符串编码时需要用的字节长度
    - (NSUInteger)lengthOfBytesUsingEncoding:(NSStringEncoding)enc; // Result in O(n) time; the result is exact. Returns 0 on error (cannot convert to specified encoding, or overflow)字符串编码时需要用到最大字节长度
    
    FormC 指示使用完全规范化分解对 Unicode 字符串进行规范化,然后将序列替换为其原复合字符(如果可能)。FormD 指示使用完全规范化分解对 Unicode 字符串进行规范化。FormKC 指示使用完全兼容性分解对 Unicode 字符串进行规范化,然后将序列替换为其原复合字符(如果可能)。FormKD 指示使用完全兼容性分解对 Unicode 字符串进行规范化。- (NSString *)decomposedStringWithCanonicalMapping;   Unicode范式D标准化
    - (NSString *)precomposedStringWithCanonicalMapping;  Unicode范式C标准化
    - (NSString *)decomposedStringWithCompatibilityMapping; Unicode范式KD标准化
    - (NSString *)precomposedStringWithCompatibilityMapping; Unicode范式KC标准化
    
    
    /* Returns a string with the character folding options applied. theOptions is a mask of compare flags with *InsensitiveSearch suffix.
    */
    - (NSString *)stringByFoldingWithOptions:(NSStringCompareOptions)options locale:(NSLocale *)locale NS_AVAILABLE(10_5, 2_0);  本地化字符串折叠
    
    
    /* Replace all occurrences of the target string in the specified range with replacement. Specified compare options are used for matching target. If NSRegularExpressionSearch is specified, the replacement is treated as a template, as in the corresponding NSRegularExpression methods, and no other options can apply except NSCaseInsensitiveSearch and NSAnchoredSearch.
    */
    - (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement options:(NSStringCompareOptions)options range:(NSRange)searchRange NS_AVAILABLE(10_5, 2_0); 字符串指定区域段替换
    
    
    /* Replace all occurrences of the target string with replacement. Invokes the above method with 0 options and range of the whole string.
    */
    - (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement NS_AVAILABLE(10_5, 2_0); 字符串替换
    
    
    /* Replace characters in range with the specified string, returning new string.
    */
    - (NSString *)stringByReplacingCharactersInRange:(NSRange)range withString:(NSString *)replacement NS_AVAILABLE(10_5, 2_0); 指定区域段字符串替换
    
    
    - (__strong const char *)UTF8String NS_RETURNS_INNER_POINTER; // Convenience to return null-terminated UTF8 representation 转化为char
    
    
    /* User-dependent encoding who value is derived from user's default language and potentially other factors. The use of this encoding might sometimes be needed when interpreting user documents with unknown encodings, in the absence of other hints.  This encoding should be used rarely, if at all. Note that some potential values here might result in unexpected encoding conversions of even fairly straightforward NSString content --- for instance, punctuation characters with a bidirectional encoding.
    */
    + (NSStringEncoding)defaultCStringEncoding; // Should be rarely used 默认C字符串编码
    
    
    + (const NSStringEncoding *)availableStringEncodings; 当前编码值
    + (NSString *)localizedNameOfStringEncoding:(NSStringEncoding)encoding;   编码的名称
    
    
    /*** Creation methods ***/
    
    
    /* In general creation methods in NSString do not apply to subclassers, as subclassers are assumed to provide their own init methods which create the string in the way the subclass wishes.  Designated initializers of NSString are thus init and initWithCoder:.
    */
    - (id)init; 初始化NSSting 对象
    - (id)initWithCharactersNoCopy:(unichar *)characters length:(NSUInteger)length freeWhenDone:(BOOL)freeBuffer; /* "NoCopy" is a hint */ 指定缓冲区,编码和字节长度初始化NSSting 对象
    - (id)initWithCharacters:(const unichar *)characters length:(NSUInteger)length; 指定unichar字符,字节长度初始化NSSting 对象
    - (id)initWithUTF8String:(const char *)nullTerminatedCString; char 转化为NSString对象
    - (id)initWithString:(NSString *)aString; 指定字符串初始化NSSting 对象
    - (id)initWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);格式化多个字符串初始化NSSting 对象
    - (id)initWithFormat:(NSString *)format arguments:(va_list)argList NS_FORMAT_FUNCTION(1,0); 格式化字符串初始化NSSting 对象
    - (id)initWithFormat:(NSString *)format locale:(id)locale, ... NS_FORMAT_FUNCTION(1,3);本地化格式化多个字符串初始化NSSting 对象
    - (id)initWithFormat:(NSString *)format locale:(id)locale arguments:(va_list)argList NS_FORMAT_FUNCTION(1,0);本地化格式化字符串初始化NSSting 对象
    - (id)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding; 指定编码转化NSData数据
    - (id)initWithBytes:(const void *)bytes length:(NSUInteger)len encoding:(NSStringEncoding)encoding; 指定编码和字节数初始化NSString对象
    - (id)initWithBytesNoCopy:(void *)bytes length:(NSUInteger)len encoding:(NSStringEncoding)encoding freeWhenDone:(BOOL)freeBuffer;指定缓冲区,编码和字节数初始化NSString对象
    
    
    + (id)string;  初始化空string对象
    + (id)stringWithString:(NSString *)string;初始化string对象
    + (id)stringWithCharacters:(const unichar *)characters length:(NSUInteger)length; 返回指定长度unichar的C字符串
    + (id)stringWithUTF8String:(const char *)nullTerminatedCString; 转化C字符串为UTF8串
    + (id)stringWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2); 格式化出初始化NSString对象,自动释放内存
    + (id)localizedStringWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);  本地化格式化出初始化NSString对象,自动释放内存
    
    
    - (id)initWithCString:(const char *)nullTerminatedCString encoding:(NSStringEncoding)encoding;   指定编码初始化C字符串,需要手动释放内存
    + (id)stringWithCString:(const char *)cString encoding:(NSStringEncoding)enc;  指定编码初始化C字符串,自动释放内存
    
    
    /* These use the specified encoding.  If nil is returned, the optional error return indicates problem that was encountered (for instance, file system or encoding errors).
    */
    - (id)initWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error; 指定编码读取URL地址数据转换为字符串,需要手动释放内存(已知编码)
    - (id)initWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error; 指定编码读取FILE地址数据转换为字符串,需要手动释放内存(已知编码)
    + (id)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error;   指定编码读取URL地址数据转换为字符串,自动释放内存(已知编码)
    + (id)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error;指定编码读取FILE地址数据转换为字符串,自动释放内存(已知编码)
    
    
    /* These try to determine the encoding, and return the encoding which was used.  Note that these methods might get "smarter" in subsequent releases of the system, and use additional techniques for recognizing encodings. If nil is returned, the optional error return indicates problem that was encountered (for instance, file system or encoding errors).
    */
    - (id)initWithContentsOfURL:(NSURL *)url usedEncoding:(NSStringEncoding *)enc error:(NSError **)error;指定编码读取URL地址数据转换为字符串,需要手动释放内存(未知编码)
    - (id)initWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc error:(NSError **)error;指定编码读取FILE地址数据转换为字符串,需要手动释放内存(未知编码)
    + (id)stringWithContentsOfURL:(NSURL *)url usedEncoding:(NSStringEncoding *)enc error:(NSError **)error;指定编码读取URL地址数据转换为字符串,自动释放内存(未知编码)
    + (id)stringWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc error:(NSError **)error;指定编码读取FILE地址数据转换为字符串,自动释放内存(未知编码)
    
    
    /* Write to specified url or path using the specified encoding.  The optional error return is to indicate file system or encoding errors.
    */
    - (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError **)error; 指定编码把数据写入文件
    - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError **)error; 指定编码把数据写入文件
    
    
    @end
    
    
    
    
    @interface NSMutableString : NSString
    
    
    /* NSMutableString primitive (funnel) method. See below for the other mutation methods.
    */
    - (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)aString; 使用 aString 替换 range 指定的区域段
    
    
    @end
    
    
    @interface NSMutableString (NSMutableStringExtensionMethods)
    
    
    - (void)insertString:(NSString *)aString atIndex:(NSUInteger)loc; 以索引loc 为起始位置插入 aString
    - (void)deleteCharactersInRange:(NSRange)range; 删除指定区域段字符
    - (void)appendString:(NSString *)aString; 追加字符串aString
    - (void)appendFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2); 追加多个字符串(格式化)
    - (void)setString:(NSString *)aString;  设置新的 aString
    
    
    /* In addition to these two, NSMutableString responds properly to all NSString creation methods.
    */
    - (id)initWithCapacity:(NSUInteger)capacity; 初始化一个容量为capacity的字符串,需要手动释放内存
    + (id)stringWithCapacity:(NSUInteger)capacity;初始化一个容量为capacity的字符串,自动释放内存( 暂且分配指定容量,用于内存优化,可以大于设置的容量)
    
    
    /* This method replaces all occurrences of the target string with the replacement string, in the specified range of the receiver string, and returns the number of replacements. NSBackwardsSearch means the search is done from the end of the range (the results could be different); NSAnchoredSearch means only anchored (but potentially multiple) instances will be replaced. NSLiteralSearch and NSCaseInsensitiveSearch also apply. NSNumericSearch is ignored. Use NSMakeRange(0, [receiver length]) to process whole string. If NSRegularExpressionSearch is specified, the replacement is treated as a template, as in the corresponding NSRegularExpression methods, and no other options can apply except NSCaseInsensitiveSearch and NSAnchoredSearch.
    */
    - (NSUInteger)replaceOccurrencesOfString:(NSString *)target withString:(NSString *)replacement options:(NSStringCompareOptions)options range:(NSRange)searchRange;
    指定限制条件(大小忽略等)指定区段中的replacement替换成target
    
    @end
    
    
    
    
    
    
    @interface NSString (NSExtendedStringPropertyListParsing)
    
    
    /* These methods are no longer recommended since they do not work with property lists and strings files in binary plist format. Please use the APIs in NSPropertyList.h instead.
    */
    - (id)propertyList;字符串转化为属性列表
    - (NSDictionary *)propertyListFromStringsFileFormat;字符串转化为字典
    
    
    @end
    
    
    
    
    
    
    @interface NSString (NSStringDeprecated)
    
    
    /* The following methods are deprecated and will be removed from this header file in the near future. These methods use [NSString defaultCStringEncoding] as the encoding to convert to, which means the results depend on the user's language and potentially other settings. This might be appropriate in some cases, but often these methods are misused, resulting in issues when running in languages other then English. UTF8String in general is a much better choice when converting arbitrary NSStrings into 8-bit representations. Additional potential replacement methods are being introduced in NSString as appropriate.
    */
    - (const char *)cString NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
    - (const char *)lossyCString NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
    - (NSUInteger)cStringLength NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
    - (void)getCString:(char *)bytes NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
    - (void)getCString:(char *)bytes maxLength:(NSUInteger)maxLength NS_DEPRECATED(10_0, 10_4, 2_0, 2_0); 
    - (void)getCString:(char *)bytes maxLength:(NSUInteger)maxLength range:(NSRange)aRange remainingRange:(NSRangePointer)leftoverRange NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
    
    
    - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
    - (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)atomically NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
    
    
    - (id)initWithContentsOfFile:(NSString *)path NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
    - (id)initWithContentsOfURL:(NSURL *)url NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
    + (id)stringWithContentsOfFile:(NSString *)path NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
    + (id)stringWithContentsOfURL:(NSURL *)url NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
    
    
    - (id)initWithCStringNoCopy:(char *)bytes length:(NSUInteger)length freeWhenDone:(BOOL)freeBuffer NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
    - (id)initWithCString:(const char *)bytes length:(NSUInteger)length NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
    - (id)initWithCString:(const char *)bytes NS_DEPRECATED(10_0, 10_4, 2_0, 2_0); 
    + (id)stringWithCString:(const char *)bytes length:(NSUInteger)length NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
    + (id)stringWithCString:(const char *)bytes NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
    
    
    /* This method is unsafe because it could potentially cause buffer overruns. You should use -getCharacters:range: instead.
    */
    - (void)getCharacters:(unichar *)buffer;
    
    
    @end
    
    
    enum {
        NSProprietaryStringEncoding = 65536    /* Installation-specific encoding */
    };
    
    
    
    
    
    
    /* The rest of this file is bookkeeping stuff that has to be here. Don't use this stuff, don't refer to it.
    */
    #if !defined(_OBJC_UNICHAR_H_)
    #define _OBJC_UNICHAR_H_
    #endif
    #define NS_UNICHAR_IS_EIGHT_BIT 0
    
    
    @interface NSSimpleCString : NSString {
    @package
        char *bytes;
        int numBytes;
    #if __LP64__
        int _unused;
    #endif
    }
    @end
    
    
    @interface NSConstantString : NSSimpleCString
    @end
    
    
    #if __LP64__
    #else
    extern void *_NSConstantStringClassReference;
    #endif


    一、NSString
    1、 - (NSUInteger)length; 返回字符串的长度
    NSString *demo = @"hello world";
    NSLog(@"%ld", [demo length]);
    2013-02-27 08:09:38.769 NSStringDemo[711:303] 11

    2、 - (unichar)characterAtIndex:(NSUInteger)index; 返回在字符串中的某个位置的字符
    NSLog(@"%C", [demo characterAtIndex: 2]);
    2013-02-27 08:17:30.078 NSStringDemo[897:303] e

    3、- (void)getCharacters:(unichar *)buffer range:(NSRange)aRange; 截取字符串指定段,返回类型unichar
     unichar buffer[6];
    [demo getCharacters:buffer range:NSMakeRange(3, 6)];
    NSLog(@"%C", buffer[5]);
    2013-02-27 08:44:27.864 NSStringDemo[1041:303] r

    4、- (NSString *)substringFromIndex:(NSUInteger)from;  截取指定位置后面的字符串
    NSLog(@"%@", [demo substringFromIndex:3]);
    2013-02-27 08:52:36.552 NSStringDemo[1292:303] lo world

    5、- (NSString *)substringToIndex:(NSUInteger)to;  截取指定长度的字符串,从索引0开始
    NSLog(@"%@", [demo substringToIndex:3]);
    2013-02-27 08:57:18.600 NSStringDemo[1384:303] hel

    6、- (NSString *)substringWithRange:(NSRange)range;   截取字符串指定段,返回类型NSString
    NSLog(@"%@", [demo substringWithRange:NSMakeRange(2, 3)]);
    2013-02-27 09:02:36.494 NSStringDemo[1466:303] llo

    7、- (NSComparisonResult)compare:(NSString *)string;   比较字符串
    NSString *demoTwo = @"good morning";
    NSLog(@"%ld", [demo compare:demoTwo]);  比较字符串
    NSLog(@"%ld", [demoTwo compare:@"Good Morning" options:NSLiteralSearch]); 以某些条件比较字符串

    NSLog(@"%ld", [demoTwo compare:@"good morning" options:NSCaseInsensitiveSearch range:NSMakeRange(3, 2)]);  以某些条件比较字符串至指定段
    NSLocale *locale = [NSLocale currentLocale];
    NSLog(@"%ld", [demoTwo compare:@"good morning" options:NSCaseInsensitiveSearch range:NSMakeRange(3, 2)  某些限制+本地化(语言环境)比较条件比较字符串至指定段

    2013-02-27 10:28:48.365 NSStringDemo[2088:303] 1
    2013-02-27 10:28:48.366 NSStringDemo[2088:303] 1
    2013-02-27 10:51:43.548 NSStringDemo[2347:303] -1
    2013-02-27 10:51:43.549 NSStringDemo[2347:303] -1

    8、- (NSComparisonResult)caseInsensitiveCompare:(NSString *)string; 不区别大小比较
    NSLog(@"%ld", [demoTwo caseInsensitiveCompare:@"Good Morning"]);
    2013-02-27 10:54:48.307 NSStringDemo[2370:303] 0

    9、- (NSComparisonResult)localizedCompare:(NSString *)string; 本地化比较
    NSLog(@"%ld", [demoTwo localizedCompare:@"大家早"]);
    2013-02-27 11:02:28.576 NSStringDemo[2442:303] -1

    10、- (NSComparisonResult)localizedCaseInsensitiveCompare:(NSString *)string; 不区别大小本地化比较
    NSLog(@"%ld", [demoTwo localizedCaseInsensitiveCompare:@"大家早"]);
    2013-02-27 11:02:28.576 NSStringDemo[2442:303] -1

    11、- (BOOL)isEqualToString:(NSString *)aString; 测试两个字符串是否相等
    NSLog(@"%d", [demoTwo isEqualToString:@"Good Morning"]);
    2013-02-27 19:06:37.980 NSStringDemo[623:303] 0



    12、- (BOOL)hasPrefix:(NSString *)aString;  测试字符串是否以 nsstring 开始

    NSLog(@"%d", [demoTwo hasPrefix:@"good"]);
    2013-02-27 19:17:55.088 NSStringDemo[766:303] 1

    13、- (BOOL)hasSuffix:(NSString *)aString;  测试字符串是否以 nsstring 结尾

    NSLog(@"%d", [demoTwo hasSuffix:@"morning"]);
    2013-02-27 19:20:04.872 NSStringDemo[814:303] 1

     

    14、- (NSRange)rangeOfString:(NSString *)aString; 搜索字符串One是否存在于字符串Two
    NSLog(@"%ld", [demoTwo rangeOfString:@"morning"].location);
    2013-02-27 19:33:37.317 NSStringDemo[951:303] 5

    15、- (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask; 以某些限制条件搜索字符串One是否存在于字符串Two
    NSLog(@"%ld", [demoTwo rangeOfString:@"Morning" options:NSCaseInsensitiveSearch].location);
    2013-02-27 19:35:49.608 NSStringDemo[994:303] 5

    16、- (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask range:(NSRange)searchRange; 以某些限制条件搜索字符串One是否存在于字符串Two指定位置段
    NSLog(@"%ld", [demoTwo rangeOfString:@"Mo" options:NSCaseInsensitiveSearch range:NSMakeRange(4, 3)].location);
    2013-02-27 19:38:32.556 NSStringDemo[1045:303] 5

    17、- (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask range:(NSRange)searchRange locale:(NSLocale *)locale NS_AVAILABLE(10_5, 2_0);以某些限制条件+本地化限制搜索字符串One是否存在于字符串Two指定位置段

    NSLog(@"%ld", [demoTwo rangeOfString:@"Mo" options:NSCaseInsensitiveSearch range:NSMakeRange(5, 2) locale:[NSLocale currentLocale]].location);
    2013-02-27 19:40:40.809 NSStringDemo[1061:303] 5


    18、- (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)aSet;指定字符集搜索
    NSCharacterSet  *iwsSet = [[[NSCharacterSet decimalDigitCharacterSet] invertedSet] retain];
    NSLog(@"%ld", [demoTwo rangeOfCharacterFromSet:iwsSet].location);
    2013-02-27 21:11:08.997 NSStringDemo[1337:303] 0


    19、- (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)aSet options:(NSStringCompareOptions)mask; 以某些限制条件指定字符集搜索
    NSLog(@"%ld", [demoTwo rangeOfCharacterFromSet:iwsSet options:NSCaseInsensitiveSearch].length);
    2013-02-27 21:17:18.600 NSStringDemo[1402:303] 1

    20、- (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)aSet options:(NSStringCompareOptions)mask range:(NSRange)searchRange; 以某些限制条件指定字符集搜索指定位置段
    NSLog(@"%ld", [demoTwo rangeOfCharacterFromSet:iwsSet options:NSLiteralSearch range:NSMakeRange(3, 3)].location);
    2013-02-27 21:17:18.600 NSStringDemo[1402:303] 3

    21、- (NSRange)rangeOfComposedCharacterSequenceAtIndex:(NSUInteger)index;  以字符串的字符编码指定索引查找位置
    NSLog(@"%ld", [demoTwo rangeOfComposedCharacterSequenceAtIndex:5].location);
    2013-02-27 21:37:57.057 NSStringDemo[1562:303] 3


    22、 -(NSRange)rangeOfComposedCharacterSequencesForRange:(NSRange)range NS_AVAILABLE(10_5, 2_0); 以字符串的字符编码指定区域段查找位置
    NSLog(@"%ld", [demoTwo rangeOfComposedCharacterSequencesForRange: NSMakeRange(3, 3)].location);
    2013-02-27 21:41:04.432 NSStringDemo[1608:303] 3


    23、- (NSString *)stringByAppendingString:(NSString *)aString; 将字符串One添加字符串Two后面
    NSLog(@"%@", [demoTwo stringByAppendingString:@" too !"]);
    2013-02-27 21:45:24.870 NSStringDemo[1647:303] good morning too !


    24、- (NSString *)stringByAppendingFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2); 将多个字符串添加字符串Two后面
    NSLog(@"%@", [demoTwo stringByAppendingFormat:@"and int is %d,string is %@--, %@--,%@--", 3, @"One", @"Two", @"Three"]);
    2013-02-27 21:48:48.759 NSStringDemo[1702:303] good morningand int is 3,string is One--, Two--,Three--


    25、类型转化- (double)doubleValue; 返回转化的double类型
    - (float)floatValue;  返回转化的float类型
    - (int)intValue;  返回转化的int类型
    - (NSInteger)integerValue NS_AVAILABLE(10_5, 2_0);  返回转化的NSInteger(

    32位系统NSInteger是一个int,即32位,但当时64位系统时,NSInteger便是64位的

    )类型
    - (long long)longLongValue NS_AVAILABLE(10_5, 2_0); 返回转化的 长int类型
    - (BOOL)boolValue NS_AVAILABLE(10_5, 2_0);   返回转化的 长BOOL类型

    NSString *demoThree = @"3.343demo";
    NSLog(@"%f", [demoThree doubleValue]);
    NSLog(@"%f", [demoThree floatValue]);
    NSLog(@"%d", [demoThree intValue]);
    NSLog(@"%ld", [demoThree integerValue]);
    NSLog(@"%ld", [demoThree longLongValue]);
    NSLog(@"%d", [demoThree boolValue]);

    2013-02-27 21:57:57.686 NSStringDemo[1773:303] 3.343000
    2013-02-27 21:57:57.687 NSStringDemo[1773:303] 3.343000
    2013-02-27 21:57:57.697 NSStringDemo[1773:303] 3
    2013-02-27 21:57:57.697 NSStringDemo[1773:303] 3
    2013-02-27 21:57:57.698 NSStringDemo[1773:303] 3
    2013-02-27 21:57:57.699 NSStringDemo[1773:303] 1

    26、- (NSArray *)componentsSeparatedByString:(NSString *)separator; 字符串转化为数组
    NSString *demoFour = [[NSString alloc] initWithString:@"One, Two, Three, Four, Five"];
    NSLog(@"%@", [[demoFour componentsSeparatedByString:@","] objectAtIndex: 3]);
    2013-02-27 22:16:22.281 NSStringDemo[1851:303]  Four

    27、- (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator NS_AVAILABLE(10_5, 2_0);  依据字符编码,分割字符串
    NSLog(@"%@", [[demoFour componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]] objectAtIndex:2]);
    2013-02-27 22:16:22.282 NSStringDemo[1851:303]  Three
     
     

    一、大小写转换
    1、- (NSString *)uppercaseString; 所有字符转化为大写
    2、- (NSString *)lowercaseString; 所有字符转化为小写
    3、- (NSString *)capitalizedString; 所有单词首字母转化大写

    NSLog(@"%@", [demoTwo uppercaseString]);
    NSLog(@"%@", [demoTwo lowercaseString]);
    NSLog(@"%@", [demoTwo capitalizedString]);


    2013-02-28 09:17:43.759 NSStringDemo[424:303] GOOD MORNING
    2013-02-28 09:17:43.759 NSStringDemo[424:303] good morning
    2013-02-28 09:17:43.760 NSStringDemo[424:303] Good Morning

    二、大小写转换(本地化)
    - (NSString *)uppercaseStringWithLocale:(NSLocale *)locale NS_AVAILABLE(10_8, 6_0); 所有字符转化为大写(本地化)
    - (NSString *)lowercaseStringWithLocale:(NSLocale *)locale NS_AVAILABLE(10_8, 6_0);所有字符转化为小写(本地化)
    - (NSString *)capitalizedStringWithLocale:(NSLocale *)locale NS_AVAILABLE(10_8, 6_0); 所有单词首字母转化大写(本地化)


    NSLog(@"%@", [demoTwo uppercaseStringWithLocale:locale]);
    NSLog(@"%@", [demoTwo lowercaseStringWithLocale:locale]);
    NSLog(@"%@", [demoTwo capitalizedStringWithLocale:locale]);


    2013-02-28 09:46:19.042 demoTwo[732:11303] GOOD MORNING
    2013-02-28 09:46:19.044 demoTwo[732:11303] good morning
    2013-02-28 09:46:19.045 demoTwo[732:11303] Good Morning


    三、- (NSString *)stringByTrimmingCharactersInSet:(NSCharacterSet *)set;   字符串替换

    NSString *demoFive = [[NSString alloc] initWithString:@"good @3!3%"];
    NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"@/:;()¥「」"、[]{}#%-*+=_\|~<>$


    四、- (void)getLineStart:(NSUInteger *)startPtr end:(NSUInteger *)lineEndPtr contentsEnd:(NSUInteger *)contentsEndPtr forRange:(NSRange)range; 返回指定开始索引到结束索引,指定段的字符串

    NSUInteger lineStart,lineEnd,contentsEnd;
    [demoFive getLineStart:&lineStart end:&lineEnd contentsEnd:&contentsEnd forRange:NSMakeRange(4, 3)];
    NSLog(@"%@", [NSString stringWithFormat:@"lineStart=%lu, lineEnd=%lu, contentsEnd=%lu", lineStart, lineEnd, contentsEnd]);
    2013-02-28 11:57:19.251 NSStringDemo[1843:303] lineStart=0, lineEnd=11, contentsEnd=11

    五、- (NSRange)lineRangeForRange:(NSRange)range;返回字符串指定段的位置和长度 ? 
    NSLog(@"%ld", [demoTwo lineRangeForRange:NSMakeRange(7, 2)].length);
    2013-02-28 11:32:44.667 NSStringDemo[1664:303] 12


    六、- (void)getParagraphStart:(NSUInteger *)startPtr end:(NSUInteger *)parEndPtr contentsEnd:(NSUInteger *)contentsEndPtr forRange:(NSRange)range;  指定段分段取字符串

    NSUInteger paraStart, paraEnd, contEnd;
    NSString *aString1 = @"Appleu2028Orangeu2029Banana Lemon";
    [aString1 getParagraphStart:&paraStart end:&paraEnd contentsEnd:&contEnd forRange:NSMakeRange(14, 1)] ;
    NSLog(@"%@", [NSString stringWithFormat:@"ParagraphStart=%d, ParagraphEnd=%d, ContentsEnd=%d", paraStart, paraEnd, contEnd]);

    2013-02-28 11:40:46.091 NSStringDemo[1720:303] ParagraphStart=13, ParagraphEnd=21, ContentsEnd=19

    七、- (NSRange)paragraphRangeForRange:(NSRange)range; 指定段分段的位置和长度
    NSRange range = [aString1 paragraphRangeForRange:NSMakeRange(3,5)];
    NSLog(@"%lu-%lu",range.location, range.length);


    八、- (NSString *)description;  返回字符串
    NSLog(@"%@", [demoTwo description]);
    2013-02-28 16:57:32.312 NSStringDemo[794:303] good morning

    九、- (NSUInteger)hash;  返回字符串哈希地址
    NSLog(@"%lu", [demoTwo hash]);
    2013-02-28 17:01:32.032 NSStringDemo[820:303] 16759986921038887375

    十、编码处理
    - (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]);

    2013-02-28 17:10:21.211 NSStringDemo[916:303] 30
    2013-02-28 17:10:21.212 NSStringDemo[916:303] 30
    2013-02-28 17:26:44.219 NSStringDemo[999:303] <676f6f64 206d6f72 6e696e67>
    2013-02-28 17:26:44.226 NSStringDemo[999:303] <676f6f64 206d6f72 6e696e67>
    2013-02-28 17:26:44.227 NSStringDemo[999:303] 1

     

    一、+ (id)string; 初始化空string对象

    NSMutableString *demoEight = [NSMutableString string];
    [demoEight appendString:@"demo"];
    NSLog(@"%@", demoEight);
    2013-03-02 19:57:42.588 NSStringDemo[1536:303] demo



    二、+ (id)stringWithString:(NSString *)string;初始化string对象

    NSLog(@"%@", [NSString stringWithString:@"demoTwo"]);
    2013-03-02 20:14:42.449 NSStringDemo[1606:303] demoTwo

    三、+ (id)stringWithCharacters:(const unichar *)characters length:(NSUInteger)length; 返回指定长度unichar的C字符串const unichar chr[] = {0x53C3,'x','x'};
    NSLog(@"%@", [NSString stringWithCharacters:(const unichar *)&chr length:3]);
    2013-03-02 21:34:48.145 NSStringDemo[1763:303] 參xx



    四、+ (id)stringWithUTF8String:(const char *)nullTerminatedCString; 转化C字符串为UTF8串

    NSLog(@"%@", [NSString stringWithUTF8String:"123124"]);
    2013-03-03 08:51:26.045 NSStringDemo[615:303] ST



    五、+ (id)stringWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2); 格式化出初始化NSString对象,自动释放内存
     NSLog(@"%@", [NSString stringWithFormat:@"123DEMO"]);
    2013-03-03 08:59:23.577 NSStringDemo[648:303] SDEMO

     

    六、+ (id)localizedStringWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2); 本地化格式化出初始化NSString对象,自动释放内存

    NSLog(@"%@", [NSString localizedStringWithFormat:@"%@ %f", @"123生活ねえ、DmmoTwo", 1334.56]);
    2013-03-03 09:12:30.719 NSStringDemo[765:303] S生活ねえ、DmmoTwo 1334.560000


    七、- (id)initWithCString:(const char *)nullTerminatedCString encoding:(NSStringEncoding)encoding; 指定编码初始化C字符串,需要手动释放内存
    NSString *demoGo = [[NSString alloc] initWithFormat:@"%@", @"123生活ねえ、DmmoTwo"];
     NSLog(@"%@", [[NSString alloc] initWithCString: [demoGo UTF8String] encoding:NSUTF8StringEncoding])
    2013-03-03 09:32:15.170 NSStringDemo[1088:303] S生活ねえ、DmmoTwo


    八、+ (id)stringWithCString:(const char *)cString encoding:(NSStringEncoding)enc;  指定编码初始化C字符串,自动释放内存
    NSLog(@"%@", [NSString stringWithCString:[demoGo UTF8String] encoding:NSUTF8StringEncoding]);
    2013-03-03 09:33:30.704 NSStringDemo[1103:303] S生活ねえ、DmmoTwo


    九、- (id)initWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error; 指定编码读取URL地址数据转换为字符串,需要手动释放内存(已知编码)

    NSURL *urlPath = [NSURL URLWithString: @"http://www.baidu.com"];
    NSError *error = nil;
    NSString *urlString = [[NSString alloc] initWithContentsOfURL: urlPath encoding:NSUTF8StringEncoding error:&error];
    NSLog(@"%ld", [urlString length]);
    2013-03-03 09:57:36.917 NSStringDemo[1223:303] 16450

    十、- (id)initWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error;  指定编码读取FILE地址数据转换为字符串,需要手动释放内存(已知编码)


    NSString *fileInitOne = [[NSString alloc] initWithContentsOfFile:@"/Users/fantom/work/ios/NSStringDemo/demo.xml" encoding:NSUTF8StringEncoding  error:&error];
    NSLog(@"%ld", [fileInitOne length]);
    2013-03-05 15:06:00.301 NSStringDemo[1015:303] 1483


    十一、+ (id)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error;    指定编码读取URL地址数据转换为字符串,自动释放内存(已知编码)
    NSString *urlStringTwo = [NSString stringWithContentsOfURL:urlPath encoding:NSUTF8StringEncoding error:&error];
    NSLog(@"%ld", [urlStringTwo length]);
    2013-03-03 10:00:30.832 NSStringDemo[1251:303] 16450


    十二、+ (id)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error; 指定编码读取FILE地址数据转换为字符串,自动释放内存(已知编码)

    NSString *fileStringOne = [NSString stringWithContentsOfFile:@"/Users/fantom/NSStringDemo/demo.xml" encoding:NSUTF8StringEncoding error:&error];
    NSLog(@"%ld", [fileStringOne length]);
    2013-03-05 14:44:48.609 NSStringDemo[949:303] 1483


    十三、- (id)initWithContentsOfURL:(NSURL *)url usedEncoding:(NSStringEncoding *)enc error:(NSError **)error; 指定编码读取URL地址数据转换为字符串,需要手动释放内存(未知编码)
    NSStringEncoding encoding = 0;

    NSString *urlUserEncodeInitString = [[NSString alloc] initWithContentsOfURL:urlPath usedEncoding:&encoding error:&error];
    NSLog(@"user----%ld", [urlUserEncodeInitString length]);

    2013-03-05 15:15:09.650 NSStringDemo[1098:303] user----16721


    十四、- (id)initWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc error:(NSError **)error;指定编码读取FILE地址数据转换为字符串,需要手动释放内存(未知编码)
    NSString *fileUserEncodeInit = [[NSString alloc] initWithContentsOfFile:@"/Users/fantom/work/ios/NSStringDemo/demo.xml" usedEncoding:&encoding error:&error];
    NSLog(@"user---%ld", [fileUserEncodeInit length]); 
    2013-03-05 15:27:32.263 NSStringDemo[1192:303] user---1483


    十五、+ (id)stringWithContentsOfURL:(NSURL *)url usedEncoding:(NSStringEncoding *)enc error:(NSError **)error;指定编码读取URL地址数据转换为字符串,自动释放内存(未知编码)
    NSString *urlUserEncodeStrString = [NSString stringWithContentsOfURL:urlPath usedEncoding:&encoding error:&error];
    NSLog(@"user--%ld", [urlUserEncodeStrString length]);
    2013-03-05 15:20:19.219 NSStringDemo[1149:303] user--16721

    十六、+ (id)stringWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc error:(NSError **)error;指定编码读取FILE地址数据转换为字符串,自动释放内存(未知编码)
    NSString *fileUserEncodeStrString = [NSString stringWithContentsOfFile:@"/Users/fantom/work/ios/NSStringDemo/demo.xml" usedEncoding:&encodingerror:&error];
    NSLog(@"user---%ld", [fileUserEncodeStrString length]);
    2013-03-05 15:27:32.264 NSStringDemo[1192:303] user---1483

    十七、- (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError **)error; 指定编码把数据写入文件

    NSString *writeFilePath = @"/Users/fantom/work/ios/NSStringDemo/demo.txt";
    NSURL *url = [[NSURL alloc] initFileURLWithPath:[writeFilePath stringByExpandingTildeInPath]];
    NSString *writeUrlString = @"demo--urlString";
    NSLog(@"%d", [writeUrlString writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:&error]);

    2013-03-05 16:11:10.574 NSStringDemo[1404:303] 1


    十八、- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError **)error;指定编码把数据写入文件
    NSString *writeFileString = @"demo---fileString";
    NSLog(@"%d", [writeFileString writeToFile:writeFilePath atomically:YES encoding:NSUTF8StringEncoding error:&error]);


    2013-03-05 16:20:28.400 NSStringDemo[1461:303] 1

     

    1、- (const char *)cString NS_DEPRECATED(10_0, 10_4, 2_0, 2_0); 转化为C字符串
    NSString *str = [NSString stringWithString:@"5354"];
    cPoit = [str cString];
    NSLog(@"%s", cPoit); 
    2013-03-06 08:29:41.991 NSStringDemo[614:303] +,

    2、- (const char *)lossyCString NS_DEPRECATED(10_0, 10_4, 2_0, 2_0); 非无损转化为C字符串
    NSLog(@"%s", [str lossyCString]);
    2013-03-06 09:11:57.841 NSStringDemo[897:303] kl

    3、- (NSUInteger)cStringLength NS_DEPRECATED(10_0, 10_4, 2_0, 2_0); 返回转化C字符串后的长度
    NSLog(@"%lu", [str cStringLength]);
    2013-03-06 09:12:59.052 NSStringDemo[920:303] 3

    4、- (void)getCString:(char *)bytes NS_DEPRECATED(10_0, 10_4, 2_0, 2_0); 转化C字符串对象
    NSString *strTwo = [NSString stringWithString:@"DEMO --"];
    char cBytes[20];
    [strTwo getCString:&cBytes];
    NSLog(@"%c", cBytes[3]);
    2013-03-06 09:21:22.081 NSStringDemo[1142:303] O

    5、- (void)getCString:(char *)bytes maxLength:(NSUInteger)maxLength NS_DEPRECATED(10_0, 10_4, 2_0, 2_0); 指定缓冲区转化C字符串对象
    [strTwo getCString:&cBytes maxLength:8];
    NSLog(@"%s", cBytes);
    2013-03-06 09:30:59.580 NSStringDemo[1452:303] DEMO--I=

    6、- (void)getCString:(char *)bytes maxLength:(NSUInteger)maxLength range:(NSRange)aRange remainingRange:(NSRangePointer)leftoverRange NS_DEPRECATED(10_0, 10_4, 2_0, 2_0); 指定缓冲区转化C字符串对象
    [strTwo getCString:&cBytes maxLength:8 range:NSMakeRange(4, 3) remainingRange:nil];
    NSLog(@"%s", cBytes);
    2013-03-06 09:32:31.508 NSStringDemo[1478:303] --I

    7、- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);   以原子方式数据写入文件
    NSString *writeFilePath = @"/Users/fantom/work/ios/NSStringDemo/demo.txt";
    NSString *writeFileString = @"demo---write";
    NSLog(@"%d", [writeFileString writeToFile:writeFilePath atomically:YES]);


    8、- (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)atomically NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);  以原子方式数据写入指定URl
    NSURL *urlPath = [[NSURL alloc] initFileURLWithPath:[writeFilePath stringByExpandingTildeInPath]];NSLog(@"%d", [@"DEMO---URL" writeToURL:urlPath atomically:NO]);
    2013-03-06 10:05:01.489 NSStringDemo[1656:303] 1

    9、- (id)initWithContentsOfFile:(NSString *)path NS_DEPRECATED(10_0, 10_4, 2_0, 2_0); 读取FILE地址数据,需要手动释放内存
    NSLog(@"%@", [[NSString alloc] initWithContentsOfFile:writeFilePath]);
    2013-03-06 10:15:15.995 NSStringDemo[1708:303] DEMO---URL


    10、- (id)initWithContentsOfURL:(NSURL *)url NS_DEPRECATED(10_0, 10_4, 2_0, 2_0); 读取URL地址数据,自动释放内存
    NSLog(@"%@", [[NSString alloc] initWithContentsOfURL:urlPath]);
    2013-03-06 10:15:15.994 NSStringDemo[1708:303] DEMO---URL

    11、+ (id)stringWithContentsOfFile:(NSString *)path NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);读取FILE地址数据,需要手动释放内存
    NSLog(@"%@", [NSString stringWithContentsOfFile:writeFilePath]);
    2013-03-06 10:17:03.528 NSStringDemo[1720:303] DEMO---URL

    12、+ (id)stringWithContentsOfURL:(NSURL *)url NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);读取URL地址数据,自动释放内存
    NSLog(@"%@", [NSString stringWithContentsOfURL:urlPath]);
    2013-03-06 10:17:03.527 NSStringDemo[1720:303] DEMO---URL

    13、- (id)initWithCStringNoCopy:(char *)bytes length:(NSUInteger)length freeWhenDone:(BOOL)freeBuffer NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);指定缓冲区,字节长度初始化CSting,需要手动释放内存

    char *cChar = "demoGO";
     NSLog(@"%@", [[NSString alloc] initWithCStringNoCopy:cChar length:2 freeWhenDone:NO]);
    2013-03-06 10:27:23.438 NSStringDemo[1906:303] de

    14、- (id)initWithCString:(const char *)bytes length:(NSUInteger)length NS_DEPRECATED(10_0, 10_4, 2_0, 2_0); 指定字节长度初始化CSting ,需要手动释放内存
    const char * constChar = "go--go";
    NSLog(@"%@", [[NSString alloc] initWithCString:constChar length:5]);
    2013-03-06 10:31:22.460 NSStringDemo[1972:303] go--g

    15、- (id)initWithCString:(const char *)bytes NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);初始化CSting ,需要手动释放内存
     NSLog(@"%@", [[NSString alloc] initWithCString:constChar]);
    2013-03-06 10:32:40.617 NSStringDemo[1985:303] go--go

    16、+ (id)stringWithCString:(const char *)bytes length:(NSUInteger)length NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);指定字节长度初始化CSting ,自动释放内存
    NSLog(@"%@", [NSString stringWithCString:constChar length:5]);
    2013-03-06 10:36:48.138 NSStringDemo[2068:303] go--g


    17、+ (id)stringWithCString:(const char *)bytes NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);初始化CSting ,自动释放内存
    NSLog(@"%@", [NSString stringWithCString:constChar]);  
    2013-03-06 10:37:44.894 NSStringDemo[2086:303] go--go

     
  • 相关阅读:
    Codeforces Round #455 (Div. 2) A. Generate Login【贪心】
    Codeforces Round #315 (Div. 2)【贪心/重排去掉大于n的元素和替换重复的元素】
    CSU-ACM2018寒假集训选拔-入门题
    Codeforces Round #454 C. Shockers【模拟/hash】
    Nowcoder Girl 参考题解【待写】
    2017吉首大学新生赛
    P1450 包裹快递 RP+14【二分】
    NewCode
    2017年浙江工业大学大学生程序设计迎新赛决赛题解
    Codeforces Round #451 (Div. 2) B. Proper Nutrition【枚举/扩展欧几里得/给你n问有没有两个非负整数x,y满足x·a + y·b = n】
  • 原文地址:https://www.cnblogs.com/liuxiaokun/p/5544810.html
Copyright © 2011-2022 走看看