微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Objective-C ,ios,iphone开发基础:几个常用类-NSString

原文链接http://www.cnblogs.com/wsq724439564/p/3273354.html

一个字符串:

//必须在字符串的前面加上@符号, 
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

 

转载于:https://www.cnblogs.com/wsq724439564/p/3273354.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐