• The foundation framework provides many useful classes, such as nsstring: String nsarray: array nsdictionary: dictionary nsdate: Date nsdata: Data nsnumber: Number 1. first, we will introduce nsstringa. nsstring creation 1) the most direct method (this is a constant string)
Nsstring * STR = @ "hehe ";
2) Format
Nsstring * STR = [nsstring stringwithformat: @ "My age is % d", 10];
Nsstring * STR = [[nsstring alloc] initwithformat: @ "My age is % d", 10];
3) read from the file
// Used to save the error message
Nserror * error = nil;
// Read the file content
Nsstring * STR = [nsstring stringwithcontentsoffile: @ "/users/MJ/desktop/test.txt" encoding: nsutf8stringencoding error: & error];
// If an error message exists
If (error ){
Nslog (@ "read failed, error cause: % @", [error localizeddescription]);
} Else {// if no error message exists
Nslog (@ "read Successful, file content: \ n % @", STR );
}
4) read from the URL
Nserror * error = nil;
Nsurl * url = [nsurl urlwithstring: @ "file: // users/MJ/desktop/test.txt"];
Nsstring * STR = [nsstring stringwithcontentsofurl: URL encoding: nsutf8stringencoding error: & error];
If (error ){
Nslog (@ "read failed, error cause: % @", [error localizeddescription]);
} Else {
Nslog (@ "read Successful, file content: \ n % @", STR );
}
2. nsstring Storage
Nsstring * STR = @ "HAHAHA ";
[STR writetofile: @ "/users/MJ/desktop/str.txt" atomically: Yes encoding: nsutf8stringencoding error: Nil];
[STR writetourl: [nsurl urlwithstring: @ "/users/MJ/desktop/str.txt"] atomically: Yes encoding: nsutf8stringencoding error: Nil];
3. Some important methods in the nsstring header file
1) convert all characters into uppercase letters-(nsstring *) uppercasestring; 2) convert all characters into lowercase letters-(nsstring *) lowercasestring3) change the first letter to uppercase, -(nsstring *) capitalizedstring4) Comparison of strings-(bool) isw.tostring :( nsstring *) astring; 5) string search. whether to start with astring-(bool) hasprefix :( nsstring *) astring; B. whether to end with astring-(bool) hassuffix :( nsstring *) astring; C. used to check whether the string contains astring-(nsange) rangeofstring :( nsstring *) astring; 6. some methods of string truncation. starting from the specified position from (including characters at the specified position) to the end-(nsstring *) substringfromindex :( nsuinteger) from; B. it is intercepted from the beginning of the string to the specified position to, but does not include the character-(nsstring *) substringtoindex :( nsuinteger) to; C. extract the substring-(nsstring *) substringwithrange (nsange) range; D. replacement with target-(nsstring *) stringbyreplacingoccurrencesofstring :( nsstring *) Target withstring :( nsstring *) Replacement, you do not need to remember these methods. You only need to understand these methods. When using these methods, you can find the corresponding methods in the header file.
Basic Framework Foundation in oC