zoukankan      html  css  js  c++  java
  • Objective-C ,ios,iphone开发基础:几个常用类-NSString

    第一个字符串:

    //必须在字符串的前面加上@符号, 
    NSString* str=@"shouqiang_Wei";
    //输出以%@输出。 NSLog(
    @"%@",str);

    结果:
    2013-08-21 14:06:22.963 NSNumber[611:707] shouqiang_Wei
    NSString 类原型:
      1 @interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding>
      2 
      3 /* 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.
      4 */
      5 - (NSUInteger)length;            
      6 - (unichar)characterAtIndex:(NSUInteger)index;
      7 
      8 @end
      9 
     10 @interface NSString (NSStringExtensionMethods)
     11 
     12 - (void)getCharacters:(unichar *)buffer range:(NSRange)aRange;
     13 
     14 - (NSString *)substringFromIndex:(NSUInteger)from;
     15 - (NSString *)substringToIndex:(NSUInteger)to;
     16 - (NSString *)substringWithRange:(NSRange)range;    // Hint: Use with rangeOfComposedCharacterSequencesForRange: to avoid breaking up composed characters
     17 
     18 - (NSComparisonResult)compare:(NSString *)string;
     19 - (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask;
     20 - (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange;
     21 - (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange locale:(id)locale; // locale arg used to be a dictionary pre-Leopard. We now accepts NSLocale. Assumes the current locale if non-nil and non-NSLocale.
     22 - (NSComparisonResult)caseInsensitiveCompare:(NSString *)string;
     23 - (NSComparisonResult)localizedCompare:(NSString *)string;
     24 - (NSComparisonResult)localizedCaseInsensitiveCompare:(NSString *)string;
     25 
     26 /* 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.
     27 */
     28 - (NSComparisonResult)localizedStandardCompare:(NSString *)string NS_AVAILABLE(10_6, 4_0);
     29 
     30 - (BOOL)isEqualToString:(NSString *)aString;
     31 
     32 - (BOOL)hasPrefix:(NSString *)aString;
     33 - (BOOL)hasSuffix:(NSString *)aString;
     34 
     35 /* 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.
     36 */
     37 - (NSRange)rangeOfString:(NSString *)aString;
     38 - (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask;
     39 - (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask range:(NSRange)searchRange;
     40 - (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask range:(NSRange)searchRange locale:(NSLocale *)locale NS_AVAILABLE(10_5, 2_0);
     41 
     42 /* These return the range of the first character from the set in the string, not the range of a sequence of characters. 
     43 */
     44 - (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)aSet;
     45 - (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)aSet options:(NSStringCompareOptions)mask;
     46 - (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)aSet options:(NSStringCompareOptions)mask range:(NSRange)searchRange;
     47 
     48 - (NSRange)rangeOfComposedCharacterSequenceAtIndex:(NSUInteger)index;
     49 - (NSRange)rangeOfComposedCharacterSequencesForRange:(NSRange)range NS_AVAILABLE(10_5, 2_0);
     50 
     51 - (NSString *)stringByAppendingString:(NSString *)aString;
     52 - (NSString *)stringByAppendingFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);
     53 
     54 /* The following convenience methods all skip initial space characters (whitespaceSet) and ignore trailing characters. NSScanner can be used for more "exact" parsing of numbers.
     55 */
     56 - (double)doubleValue;
     57 - (float)floatValue;
     58 - (int)intValue;
     59 - (NSInteger)integerValue NS_AVAILABLE(10_5, 2_0);
     60 - (long long)longLongValue NS_AVAILABLE(10_5, 2_0);
     61 - (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.
     62 
     63 - (NSArray *)componentsSeparatedByString:(NSString *)separator;
     64 - (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator NS_AVAILABLE(10_5, 2_0);
     65 
     66 - (NSString *)commonPrefixWithString:(NSString *)aString options:(NSStringCompareOptions)mask;
     67 
     68 - (NSString *)uppercaseString;
     69 - (NSString *)lowercaseString;
     70 - (NSString *)capitalizedString;
     71 
     72 - (NSString *)stringByTrimmingCharactersInSet:(NSCharacterSet *)set;
     73 - (NSString *)stringByPaddingToLength:(NSUInteger)newLength withString:(NSString *)padString startingAtIndex:(NSUInteger)padIndex;
     74 
     75 - (void)getLineStart:(NSUInteger *)startPtr end:(NSUInteger *)lineEndPtr contentsEnd:(NSUInteger *)contentsEndPtr forRange:(NSRange)range;
     76 - (NSRange)lineRangeForRange:(NSRange)range;
     77 
     78 - (void)getParagraphStart:(NSUInteger *)startPtr end:(NSUInteger *)parEndPtr contentsEnd:(NSUInteger *)contentsEndPtr forRange:(NSRange)range;
     79 - (NSRange)paragraphRangeForRange:(NSRange)range;
     80 
     81 #if NS_BLOCKS_AVAILABLE
     82 enum {
     83     // Pass in one of the "By" options:
     84     NSStringEnumerationByLines = 0,                       // Equivalent to lineRangeForRange:
     85         NSStringEnumerationByParagraphs = 1,                  // Equivalent to paragraphRangeForRange:
     86     NSStringEnumerationByComposedCharacterSequences = 2,  // Equivalent to rangeOfComposedCharacterSequencesForRange:
     87     NSStringEnumerationByWords = 3,
     88         NSStringEnumerationBySentences = 4,
     89     // ...and combine any of the desired additional options:
     90     NSStringEnumerationReverse = 1UL << 8,
     91     NSStringEnumerationSubstringNotRequired = 1UL << 9,
     92     NSStringEnumerationLocalized = 1UL << 10              // User's default locale
     93 };
     94 typedef NSUInteger NSStringEnumerationOptions;
     95 
     96 - (void)enumerateSubstringsInRange:(NSRange)range options:(NSStringEnumerationOptions)opts usingBlock:(void (^)(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop))block NS_AVAILABLE(10_6, 4_0);
     97 - (void)enumerateLinesUsingBlock:(void (^)(NSString *line, BOOL *stop))block NS_AVAILABLE(10_6, 4_0);
     98 #endif
     99 
    100 - (NSString *)description;
    101 
    102 - (NSUInteger)hash;
    103 
    104 /*** Encoding methods ***/
    105 
    106 - (NSStringEncoding)fastestEncoding;        // Result in O(1) time; a rough estimate
    107 - (NSStringEncoding)smallestEncoding;       // Result in O(n) time; the encoding in which the string is most compact
    108 
    109 - (NSData *)dataUsingEncoding:(NSStringEncoding)encoding allowLossyConversion:(BOOL)lossy;   // External representation
    110 - (NSData *)dataUsingEncoding:(NSStringEncoding)encoding;                                    // External representation
    111 
    112 - (BOOL)canBeConvertedToEncoding:(NSStringEncoding)encoding;
    113 
    114 /* 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.
    115 */
    116 - (__strong const char *)cStringUsingEncoding:(NSStringEncoding)encoding;    // "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)
    117 - (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:.)
    118 
    119 /* Use this to convert string section at a time into a fixed-size buffer, without any allocations.  Does not NULL-terminate. 
    120     buffer is the buffer to write to; if NULL, this method can be used to computed size of needed buffer.
    121     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. 
    122     usedBufferCount is the length of the buffer used up by the current conversion. Can be NULL.
    123     encoding is the encoding to convert to.
    124     options specifies the options to apply.
    125     range is the range to convert.
    126     leftOver is the remaining range. Can be NULL.
    127     YES return indicates some characters were converted. Conversion might usually stop when the buffer fills, 
    128       but it might also stop when the conversion isn't possible due to the chosen encoding. 
    129 */
    130 - (BOOL)getBytes:(void *)buffer maxLength:(NSUInteger)maxBufferCount usedLength:(NSUInteger *)usedBufferCount encoding:(NSStringEncoding)encoding options:(NSStringEncodingConversionOptions)options range:(NSRange)range remainingRange:(NSRangePointer)leftover;
    131 
    132 /* 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.
    133 */
    134 - (NSUInteger)maximumLengthOfBytesUsingEncoding:(NSStringEncoding)enc;    // Result in O(1) time; the estimate may be way over what's needed. Returns 0 on error (overflow)
    135 - (NSUInteger)lengthOfBytesUsingEncoding:(NSStringEncoding)enc;        // Result in O(n) time; the result is exact. Returns 0 on error (cannot convert to specified encoding, or overflow)
    136 
    137 - (NSString *)decomposedStringWithCanonicalMapping;
    138 - (NSString *)precomposedStringWithCanonicalMapping;
    139 - (NSString *)decomposedStringWithCompatibilityMapping;
    140 - (NSString *)precomposedStringWithCompatibilityMapping;
    141 
    142 /* Returns a string with the character folding options applied. theOptions is a mask of compare flags with *InsensitiveSearch suffix.
    143 */
    144 - (NSString *)stringByFoldingWithOptions:(NSStringCompareOptions)options locale:(NSLocale *)locale NS_AVAILABLE(10_5, 2_0);
    145 
    146 /* 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.
    147 */
    148 - (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement options:(NSStringCompareOptions)options range:(NSRange)searchRange NS_AVAILABLE(10_5, 2_0);
    149 
    150 /* Replace all occurrences of the target string with replacement. Invokes the above method with 0 options and range of the whole string.
    151 */
    152 - (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement NS_AVAILABLE(10_5, 2_0);
    153 
    154 /* Replace characters in range with the specified string, returning new string.
    155 */
    156 - (NSString *)stringByReplacingCharactersInRange:(NSRange)range withString:(NSString *)replacement NS_AVAILABLE(10_5, 2_0);
    157 
    158 - (__strong const char *)UTF8String;    // Convenience to return null-terminated UTF8 representation
    159 
    160 /* 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.
    161 */
    162 + (NSStringEncoding)defaultCStringEncoding;    // Should be rarely used
    163 
    164 + (const NSStringEncoding *)availableStringEncodings;
    165 + (NSString *)localizedNameOfStringEncoding:(NSStringEncoding)encoding;
    166 
    167 /*** Creation methods ***/
    168 
    169 /* 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:.
    170 */
    171 - (id)init;
    172 - (id)initWithCharactersNoCopy:(unichar *)characters length:(NSUInteger)length freeWhenDone:(BOOL)freeBuffer;    /* "NoCopy" is a hint */
    173 - (id)initWithCharacters:(const unichar *)characters length:(NSUInteger)length;
    174 - (id)initWithUTF8String:(const char *)nullTerminatedCString;
    175 - (id)initWithString:(NSString *)aString;
    176 - (id)initWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);
    177 - (id)initWithFormat:(NSString *)format arguments:(va_list)argList NS_FORMAT_FUNCTION(1,0);
    178 - (id)initWithFormat:(NSString *)format locale:(id)locale, ... NS_FORMAT_FUNCTION(1,3);
    179 - (id)initWithFormat:(NSString *)format locale:(id)locale arguments:(va_list)argList NS_FORMAT_FUNCTION(1,0);
    180 - (id)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding;
    181 - (id)initWithBytes:(const void *)bytes length:(NSUInteger)len encoding:(NSStringEncoding)encoding;
    182 - (id)initWithBytesNoCopy:(void *)bytes length:(NSUInteger)len encoding:(NSStringEncoding)encoding freeWhenDone:(BOOL)freeBuffer;    /* "NoCopy" is a hint */
    183 
    184 + (id)string;
    185 + (id)stringWithString:(NSString *)string;
    186 + (id)stringWithCharacters:(const unichar *)characters length:(NSUInteger)length;
    187 + (id)stringWithUTF8String:(const char *)nullTerminatedCString;
    188 + (id)stringWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);
    189 + (id)localizedStringWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);
    190 
    191 - (id)initWithCString:(const char *)nullTerminatedCString encoding:(NSStringEncoding)encoding;
    192 + (id)stringWithCString:(const char *)cString encoding:(NSStringEncoding)enc;
    193 
    194 /* 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).
    195 */
    196 - (id)initWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error;
    197 - (id)initWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error;
    198 + (id)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error;
    199 + (id)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error;
    200 
    201 /* 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).
    202 */
    203 - (id)initWithContentsOfURL:(NSURL *)url usedEncoding:(NSStringEncoding *)enc error:(NSError **)error;
    204 - (id)initWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc error:(NSError **)error;
    205 + (id)stringWithContentsOfURL:(NSURL *)url usedEncoding:(NSStringEncoding *)enc error:(NSError **)error;
    206 + (id)stringWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc error:(NSError **)error;
    207 
    208 /* Write to specified url or path using the specified encoding.  The optional error return is to indicate file system or encoding errors.
    209 */
    210 - (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError **)error;
    211 - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError **)error;
    212 
    213 @end
  • 相关阅读:
    【转】Android之Inflate()方法用途+setContentView和inflate区别
    【转】Android 面试题笔记android五种布局及其作用
    温故知新系列(一)冒泡排序
    一道Java面试题目——随想+心得
    关于String 和 StringBuffer、StringBuilder
    Mysql变量
    Mysql函数
    Mysql流程控制
    Mysql事物
    Mysql视图
  • 原文地址:https://www.cnblogs.com/wsq724439564/p/3273354.html
Copyright © 2011-2022 走看看