The Objective-c class in the core processing string is NSString and nsmutablestring, the biggest difference between the two classes is that the content and length of the string cannot be changed dynamically after nsstring creation of the assignment, unless the string is re-assigned to the value. Nsmutablestring creates an assignment to dynamically change the content and length on that string.
To Create a nsstring string:The biggest difference between NSString and char* is that NSString is a objective object, and char* is a byte array. @+ "string" is the standard usage of objective-c NSString string constants, char* created without adding @
intMain () {//Create a stringNSString *str =@"Jack"; NSString*S1 = [[NSString alloc] Initwithformat:@"Age =%d",Ten]; //C string to OC stringNSString *s2 = [[NSString alloc] initwithutf8string:"Jack"]; //OC Turn C Const Char*C1 =[S2 utf8string]; NSLog (@"%s", C1); //nsutf8stringencoding used in Chinese can use this//Pass in file path to view fileNSString *S3 = [[NSString alloc] Initwithcontentsoffile:"/users/apple/desktop/1.txt"encoding:nsutf8stringencoding Error:nil]; /*URL: Resource Path Protocol header://path file://ftp://http://www.baidu.com */
The URL needs to be Encode when the IOS program accesses the HTTP resource
Nsurl *url = [[Nsurl alloc] initwithstring:@ "File:///Users/apple/Desktop/1.txt"];
Nsurl *url = [Nsurl fileurlwithpath:@ "/users/apple/desktop/1.txt"];
*S4 = [[NSString alloc] Initwithcontentsofurl:url encoding:nsutf8stringencoding Error:nil]; NSLog (@ "s4=\n%@", S4); return 0 ;}
nsstring : Immutable string
nsmutablestring : variable string
intMain () {//create string nsmutablestring*s = [Nsmutablestring stringWithFormat:@"my Age is"]; //stitching the contents to the back of S[s appendString:@"3"]; //get the scope of isNsrange range = [s rangeofstring:@" is"]; [s deletecharactersinrange:range]; NSString*S1 = [NSString stringWithFormat:@" Age is"]; NSString*S3 = [S1 stringbyappendingstring:@"3"]; NSLog (@"s=%@, s1=%@", S, S1); return 0;}
Deleting elements in a string
Stringwithstring method: Used to create a string initialization assignment Rangeofstring method: Passing in a string returns a range within that string can also be written nsmakerange (0, 3) Meaning range in string 0 bits to 3rd bit deletecharactersinrange: Delete string argument is range is the scope of the deletion.
intMain () {//Create a stringnsmutablestring *str = [nsmutablestring stringwithstring:@"haha Hello!! "]; //delete characters with "haha" in the string[STR deletecharactersinrange: [str rangeofstring:@"haha"]]; NSLog (@"str =%@", str); return 0; }
Objective-c (Foundation frame a string)