iOS development process, from start to finish and will NSString dealing here summarize the commonly used method of NSString, the method used in the subsequent learning process will continue to add
Foundation framework is NSString class could not be variable string when NSString created, the contents of the string can not be changed, the corresponding class is NSMutableString
First, create NSString objects
1. Use a class method (stringWithFormat :) string created, this method can dynamically build a string, and is able to pass a string argument (recommended)
1 NSString * str = [NSString stringWithFormat: @ " Once I inquire about your recent life @% " , @ " Love you " ]; 2 NSLog ( @ " % @ " , str);
2. Create a method other
1 // to turn into a C string string object OC 2 char * P = " the I Love Beijing " ; . 3 NSString TestStr * = [[NSString the alloc] initWithFormat: @ " % S " , P]; . 4 NSLog ( @ " testString =% @ " , TestStr); . 5 . 6 // the OC string object into a C string . 7 const char * newp = [TestStr the UTF8String]; . 8 NSLog ( @ " newp =% S " , newp); . 9 10 // class methods and instance method format control character string corresponding to the constructed object 11 // Create using (Foundation framework) of an object class method, the autorelease pool management memory 12 is NSString * str2 = [NSString stringWithFormat: @ " This IS -OC Sting A% D " , 109 ]; 13 is NSLog ( @ " str2 % @ = " , str2); 14 // ======================================= ====== 15 // use an existing string object to construct a new string object 16 NSString STR * = @ " the I'm a OC string " ; . 17 NSString Str3 * = [[NSString the alloc] initWithString : STR]; 18 is NSLog ( @ " Str3 =% @ ", Str3); . 19 // corresponding class method 20 is NSString str4 * = [NSString stringWithString: STR]; 21 is NSLog ( @ " str4 =% @ " , str4); 22 is 23 is // ======= ======================================= 24 // use a string to build a C OC string object 25 NSString str5 * = [[NSString the alloc] initWithUTF8String: " This iS string AC " ]; 26 is NSLog ( @ " str5 =% @ " , str5); 27 // corresponding class method 28 * = STR6 NSString [NSString stringWithUTF8String: " This IS String AC " ]; 29 NSLog ( @ " STR6 =% @ " , STR6); 30 31 is // ================ ============================== 32 // contents of the file to build a string object 33 is 34 is NSString * filePath = @ " / the Users / Qianfeng / Desktop / test.txt " ; 35 / * 36 first parameter: the read path of the file 37 second parameter: set to specify the encoding NSUTF8String 38 is a third parameter: the process of reading the file used to store the error message 39 * / 40 NSError* error = nil; 41 NSString* str7 = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; 42 NSLog(@"str7 = %@",str7);
Two, NSString common operations
1 // Get string length (length) 2 NSString * str = [NSString stringWithFormat: @ " Once I inquire about your recent life @% " , @ " Love you " ]; 3 NSUInteger lenth = [str length]; . 4 NSLog ( @ " % LD " , lenth); . 5 NSLog ( @ " % @ " , STR); . 6 // NSRange . 7 / * . 8 NSRange defined as Foundation framework . 9 typedef struct {_NSRange 10 NSUInteger LOCATION; // It indicates the position . 11 NSUInteger length; // length 12 is } NSRange; 13 is * / 14 NSString * str2 = @ " busy if there are spare motion? " ; 15 NSString subString * = @ " busy " ; 16 // Find substring (0 start) . 17 NSRange Range = [str2 rangeOfString: subString]; 18 is // methods within the structure of the print frame . 19 NSLog ( @ " % @ " , NSStringFromRange (Range)); 20 is // used to create framework Range 21 is NSRange newRange = NSMakeRange ( 5, . 3 ); 22 is NSString newTemp * = [str2 substringWithRange: newRange]; 23 is NSLog ( @ " % @ " , newTemp); 24 // determines whether or not found in the parent sub-string string 25 IF (range.location == NSNotFound) { 26 is NSLog ( @ " Can Not Fount " ); 27 } the else { 28 NSLog ( @ " Got IT! " ); 29 } 30 // string comparison (size) 31 is NSString * = strA@ " Chengdu " ; 32 NSString strB * = @ " UNIVERSITATIS SCIENTIAE MEDICINAE. " ; 33 is // Compare: a method return type NSComparisonResult (enumerated value), where 34 is // NSOrderedSame represents equal NSOrderedAscending for ascending (less than) 35 // NSOrderedDescending for descending (greater than) 36 IF ([strA Compare: strB] == NSOrderedSame) { 37 [ NSLog ( @ " equal " ); 38 is } the else IF ([strA Compare: strB] == NSOrderedAscending) { 39 NSLog ( @ " less than" ); 40 } the else { 41 is NSLog ( @" greater than " ); 42 } 43 // string comparison (equality) 44 is BOOL isEqule = [strA as isEqualToString: strB]; 45 NSLog ( @" of The Result IS:% D " , isEqule); 46 is 47 // determined before the suffix string 48 NSString fileString * = @" /Users/student/DeskTop/file.txt " ; 49 // Analyzing prefix (hasPrefix :) method 50 IsPrefix = BOOL [fileString hasPrefix: @ " / the Users " ]; 51 is // Analyzing suffix (hasSuffix :) Method 52 is BOOL isSuffix = [fileString hasSuffix: @ " .txt " ]; 53 is NSLog ( @ " isPrefix% = D, isSuffix % D = " , isPrefix, isSuffix); 54 is // string into a number 55 / * 56 method string conversions framework provides the following: 57 is - (Double) doubleValue in; 58 - (a float) o floatValue; 59 - (int ) intValue; 60 - (NSInteger)integerValue NS_AVAILABLE(10_5, 2_0); 61 - (long long)longLongValue NS_AVAILABLE(10_5, 2_0); 62 - (BOOL)boolValue NS_AVAILABLE(10_5, 2_0); 63 */ 64 NSString* strToNum = @"12345.234"; 65 int result = [strToNum intValue]; 66 NSLog(@"result = %d", result);
Reproduced in: https: //www.cnblogs.com/pretty-guy/p/3974640.html