[Oc learning notes] string, oc learning notes string
1 // create a string 2 NSString * str1 = [NSString string]; 3 // create a c string 4 NSString * str2 = [NSString stringWithCString: "ss" encoding: NSUTF8StringEncoding]; 5 // create a string 6 NSString * str3 = [NSString stringWithString: @ "dd"]; 7 // create nslog () format String 8 NSString * str4 = [NSString stringWithFormat: @ "s"]; 9 // set the file content to string content 10 NSString * path = @ "/Users/XuLee/Desktop/oc/abc.txt"; // file path 11 NSError * error; // error message 12 NSString * ss = [NSString stringWithContentsOfFile: path encoding: NSUTF8StringEncoding error: & error]; 13 if (error) {14 NSLog (@ "% @", error); // print error message 15} 16 // write the string to file 17 [ss writeToFile: path atomically: YES encoding: NSUTF8StringEncoding error: & error]; 18 // print error 19 if (error) {20 NSLog (@ "% @", error ); 21} 22 23 // string initialization 24 NSString * str5 = [[NSString alloc] init]; 25 // string to initialize the string 26 NSString * str6 = [[NSString alloc] initWithString: @ "jj"]; 27 // use the c string to initialize the string 28 NSString * str7 = [[NSString alloc] initWithCString: "s" encoding: NSUTF8StringEncoding]; 29 // use nslog to initialize the string 30 NSString * str8 = [[NSString alloc] initWithFormat: @ "s"]; 31 // set the file content to string initialization 32 NSString * sss = [[NSString alloc] initWithContentsOfFile: path encoding: NSUTF8StringEncoding error: & error]; 33 if (error) {34 NSLog (@ "% @", error ); // print error message 35} 36 37 38 // string judgment and comparison 39 // determine whether the string starts to return 0 or 1 40 NSLog (@ "% zi ", [ss hasPrefix: @ "ss"]); 41 // determines whether the string returns 0 or 1 42 NSLog (@ "% zi", [ss hasSuffix: @ "s"]); 43 // compare string size return value 0 1-1 44 [ss compare: sss]; // If ss> sss returns 1 ss = sss returns 0 ss <sss returns-1 45 // the return value type is NSComparisonResult 46 // determine whether a certain string exists and returns 0 or 1 47 [ss containsString: @ "d"]; 48 49 50 51 // string case conversion generates a new string 52 // converts it to uppercase 53 [ss uppercaseString]; 54 // convert to lowercase 55 [ss lowercaseString]; 56 // all lowercase 57 except the first letter [ss capitalizedString]; 58 59 // cut the string to generate a new string 60 // cut to cut from the specified position to the end, including the specified position 61 [ss substringFromIndex: 2]; 62 // cut from the start to the specified position, excluding the specified position 63 [ss substringToIndex: 2]; 64 // cut the content within the specified range 65 [ss substringWithRange: NSMakeRange (1, 2)]; 66 67 68 // type conversion generates the corresponding type, but the string content must conform to the corresponding content 69 // integer 70 [ss intValue]; 71 // floating point 72 [ss floatValue]; 73 [ss doubleValue]; 74 75 76 // other 77 // convert the c string to the oc string 78 char s [3] = "s"; 79 NSString * oc = @ (s ); 80 // character 81 unichar c = [ss characterAtIndex: 2]; 82 // String Length 83 NSLog (@ "% zi", ss. length); 84 // Add a string after the string to generate a new string 85 [ss stringByAppendingString: @ "ss"]; 86 // check whether a certain character exists. The returned value is in the range of 87 [ss rangeOfString: @ "ss"]; 88 // if no range is found. location = nsnotfound 89 // Replace the first 90 [ss stringByReplacingOccurrencesOfString: @ "ss" withString: @ "sss"]; 91 92 93 // variable string 94 // You must assign a value to the bucket 95 NSMutableString * nss = [[NSMutableString alloc] initWithCapacity: 10] to create the bucket. 96 NSMutableString * nss2 = [NSMutableString stringWithCapacity: 10]; 97 // value 98 // Add a string 99 to the end [nss appendString: @ "s"]; 100 // Add an initialized string 101 [nss appendFormat: @ "sss % @", ss]; 102 // insert content 103 [nss insertString: @ "s" atIndex: 2]; 104 // Delete the string 105 in the specified range [nss deleteCharactersInRange: NSMakeRange (1, 2)]; 106 // use a string instead of the content in the specified range 107 [nss replaceCharactersInRange: NSMakeRange (1, 2) withString: @ "sss"];