-----<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, look forward to communicating with you! -------
Foundation framework
A framework is a collection of many classes, methods, functions, and documents that are organized in a logical way to make it easier to develop programs.
The foundation framework allows you to use some basic objects, such as numbers and strings, as well as a collection of objects, such as arrays, dictionaries, and collections, and other features such as processing dates and times, memory management, processing file systems, storing (or archiving) objects, processing geometric data structures (such as points and rectangles)
Show hidden files: Defaults write Com.apple.finder appleshowallfiles–bool true
Hide hidden files: Defaults write Com.apple.finder Appleshowallfiles–bool false
---------------the use of NSString
1). Direct Creation Method:
NSString *str = @ "abc";
2). Format creation:
NSString *st2 = [NSString stringwithformat:@ "%@", @ "Jack"]; NSString *str = [[NSString alloc] initwithformat:@ "My Age is%d", 10];
3). Text Encoding:
English: GBK (3 bytes) GB2312 (3 bytes)
UTF-8: International Universal Code (2 bytes)
4). String written to file:
Nserror *err;//Error Object
[Str writetofile:@ "/users/zhaoxiaohu/desktop/" Atomically:yes encoding:nsutf8stringencoding error:&err]; if (err! = nil) { NSLog (@ "Write Failed!,%@", err); } else{ NSLog (@ "write success"); }
5). String read from File:
Nserror *err; NSString *str = [NSString stringwithcontentsoffile:@ "/users/zhaoxiaohu/desktop/str.txt" encoding: Nsutf8stringencoding error:&err]; if (err! = nil) { NSLog (@ "read failed!,%@", err); } else{ NSLog (@ "read success"); NSLog (@ "%@", str); }
6). String comparison function:
Nscomparisonresult result = [str1 compare:str2];
Comparisons are not case-sensitive, and the number of reference characters is compared
Nscomparisonresult result = [str1 compare:str2 options:nscaseinsensitivesearch| Nsnumericsearch];
return value:
Nsorderedascending (STR1<STR2) Ascending
Nsordereddescending (STR1>STR2) Descending
Nsorderedsame (str1 = str2) equal
NSString *STR1 = @ "abc"; A NSString *str2 = @ "abc"; A //Take out the ASCII value of each character, compare the ASCII value size //compare method Compare size //compare default case-sensitive //compare This function, cannot increase the condition nscomparisonresult result = [str1 compare:str2]; When comparing, case-insensitive, number of reference characters nscomparisonresult result = [str1 compare:str2 options:nscaseinsensitivesearch| Nsnumericsearch]; Switch (Result) {case nsorderedascending: NSLog (@ "Str1 < str2 ascending"); break; Case nsordereddescending: NSLog (@ "str1 > str2 Descending"); break; Case Nsorderedsame: NSLog (@ "str1 = = str2"); break; Default: Break ; }
7). To interpret whether the strings are equal:
NSString *STR1 = @ "abc";
NSString *STR2 = @ "abc";
STR1 = = STR2 compares the address (= = compares the address of the string)
[Str1 ISEQUALTOSTRING:STR3], comparing the content, case-sensitive (this method is very common)
NSString *STR1 = @ "abc"; NSString *STR2 = @ "abc"; The comparison of the address/ /"= =" cannot determine whether the string is equal //judge whether the string content is really equal, use is //isequaltostring: //Note: He is case-sensitive if ([str1 ISEQUALTOSTRING:STR3]) { NSLog (@ "equal"); } else{ NSLog (@ "unequal"); }
8). Detect string prefix:
NSString *url = @ "HTTP://ASDFASDFASDFAASDFADSFAF";
[url hasprefix:@ "/http"]; whether the string starts with http://detection prefix
NSString *imgname = @ "logo.jpg";
[Imgname hassuffix:@ ". jpg"]; detects if the string ends with a. jpg//detection suffix
NSString *url [email protected] "HTTP://SJHASFJKFSJFHSKFSBBBBB"; if ([url hasprefix:@ "http"]) { NSLog (@ "is url"); if ([url hassuffix:@ ". jpg"]) { NSLog (@ "is picture"); } else{ NSLog (@ "not"); } }
9). Find the string:
Format:
Nsrange range = [str1 rangeofstring:str2]; Find str2 in STR1
NSString *url [email protected] "HTTP://SJHASFJKFSJFHSKFSBBBBB"; Find another string in a string where the hand position appears nsrange range = [str rangeofstring:@ "SJ"]; Nsnotfound is one of the largest long unsigned numbers //If the range.location is not found is a maximum long unsigned number //range.location indicates where the string first appears // Range.length represents the length of the substring if (range.location! = nsnotfound) { NSLog (@ "Location:%lu, Length:%lu", Range.location, range.length); } else{ NSLog (@ "no search found");
-----------------interception and substitution of strings
1, starting from the specified position from (containing the starting position) to the tail
-(NSString *) Substringfromindex: (Nsuinteger) from;
2, from the beginning of the string to the specified position to, does not contain the end position
-(NSString *) Substringtoindex: (Nsuinteger) to;
3, intercept substrings from the string according to the given Nsrange
-(NSString *) Substringwithrange: (nsrange) range;
NSString *str = @ "http://www.baidu.com"; 1) starting from XX position, to the end (contains the character of XX this position) nsstring *str1 = [str substringfromindex:5]; NSLog (@ "str1 =%@", str1); 2) from the start position, to the XX position end (does not contain xx this position) nsstring *str2 = [str substringtoindex:5]; NSLog (@ "str2 =%@", str2); 3) intercept a range of nsrange r1 = {3,4}; NSString *STR3 = [str substringwithrange:r1]; NSLog (@ "STR3 =%@", STR3);
4, String intercept exercise (get the contents of the itcast tag)
NSString *str = @ "<itcast> jobs </itcast>"; @ ">" loc+1 nsuinteger loc = [str rangeofstring:@ ">"].location + 1; "Pass" position // @ "</" loc Nsuinteger len = [str rangeofstring:@ "</"].LOCATION-LOC; The length of the character to intercept //build ruange nsrange r2 = nsmakerange (loc, Len); Intercept nsstring *substr = [str SUBSTRINGWITHRANGE:R2];
NSLog (@ "subStr =%@", subStr);
5, String substitution
STR stringbyreplacingoccurrencesofstring:@ "content in Source string" withstring:@ "to replace with new content"
NSString *str [email protected] "ASDFSDAFASDFDASDFSA"; Replace A with * nsstring *newstr = [str stringbyreplacingoccurrencesofstring:@ "a" withstring:@ "*"]; NSLog (@ "newstr =%@", newstr);
1. Get each character of a string
-(nsuinteger) length; Returns the length of a string (how many characters, whether Chinese characters, English characters, and so on, one character)
-(Unichar) Characteratindex: (Nsuinteger) index; Returns the character corresponding to the index position
NSString *str = @ "ios.itcast.cn"; 1, get the length of the string //Call string of the long method //str.length ----> [str length]; NSLog (@ "%ld", str.length); 2. Get every character of the character unsigned long len = str.length; for (int i=0; i<len; i++) { Unichar c = [str characteratindex:i]; printf ("%c ", c); }
Three, string and other data type conversions
1, and conversion of basic data types
-(double) doublevalue;
-(float) floatvalue;
-(int) intvalue;
-(BOOL) boolvalue;
NSString *s1 = @ "n"; 12+2 // s1+2; NSString *s2 = @ "2.3"; NSString *S3 = @ "345.678"; Converts a string to int n1 = [S1 intvalue]; NSLog (@ "N1 =%d", n1*34); String converted to float float f1 = [S2 floatvalue]; NSLog (@ "f1 =%f", f1+3); Converts a string to a double type double D1 = [S3 Doublevalue]; NSLog (@ "D1 =%f", d1+3);
2,c string to OC string
Char *s = "Zhangsanfeng"; NSString *str = [NSString stringwithutf8string:s];
3,oc string to C string
NSString *str2 = @ "ZBZ";
4, remove whitespace from the end of the string
NSString *str = @ "itc ast"; Nscharacterset *set =[nscharacterset Whitespacecharacterset];
Four, read and write strings through Nsurl
1,url Introduction
The full name of the URL is uniform Resource Locator (Uniform Resource Locator).
URLs are the addresses of standard resources on the Internet.
2,url format
The base URL contains: protocol, host domain name (server name \IP address), path
Example: http://ios.itcast.cn/ios/images/content_25.jpg
Can be simple to think: URL = = Protocol Header://Host domain name/path
3, read and write strings by URL
Build URLs
Nsurl *url = [Nsurl urlwithstring:@ "File:///Users/apple/Desktop/str.txt"];//need to manually add file://protocol header
Created from a file path (default is filename protocol)
Nsurl *url = [Nsurl fileurlwithpath:@ "/users/apple/desktop/str.txt"];//automatically adds the FILE://protocol header
NSString *str = @ "$10000000";
1), write string
[Str writetourl:url atomically:yes encoding:nsutf8stringencoding Error:nil]
2), reading the string
NSString *STR2 = [NSString stringwithcontentsofurl:url encoding:nsutf8stringencoding Error:nil];
Five, introduction and use of nsmutablestring
1, Nsmutablestring class inherits NSString class
The difference between 2,nsmutablestring and NSString:
NSString is immutable, the contents of the text inside can not be modified;
Nsmutablestring is variable, the text content inside can be changed at any time;
Nsmutablestring can use all methods of NSString. Inheritance
3, variable and immutable concepts:
Immutable: Refers to a string in memory occupied by the storage space fixed, and the contents of the storage can not be changed;
Variable: Refers to a string in memory occupied by the storage space can be fixed, and the stored content can be modified;
4, using:
nsmutablestring *STR2 = [nsmutablestring stringwithformat:@ "Jack"];
1), add:
[str2 appendstring:@ "&rose"];//append
2), insert a string at the specified location:
[Str insertstring:@ "p://" atindex:3];
3), delete part of the string:
[Str deletecharactersinrange:nsmakerange (3, 4)];
4), replacing part of the string
[Str replacecharactersinrange:nsmakerange (one, 5) withstring:@ "Itcast"];
Use Note:
1), cannot assign immutable string to variable string;
nsmutablestring *str = [NSString stringwithformat:@ "abc"];
The use of NSString and nsmutablestring in the framework of Dark Horse programmer--obbjective-c-foundation-my finishing