Introduction to creation and use of OC-based immutable strings: Here are the basic usage of strings.
1. Creation of strings
//To create an OC constant stringNSString *str=@"Hello world!"; NSLog (@"%@", str); //Create an OC string with a C-language string Char*ch="Hello World C"; NSString*str2=[[NSString alloc] initwithutf8string:ch]; NSLog (@"%@", STR2); //use formatting to create a string intA=Ten; NSString*str3=[[nsstring Alloc] Initwithformat:@"%@%d", Str2,a]; NSLog (@"%@", STR3); //to create a string with a class methodNSString *str4=[nsstring stringWithFormat:@"%@%@", Str2,str]; NSString*str5=[NSString Stringwithutf8string:ch]; NSLog (@"%@ , %@", STR4,STR5); //reading a file to create a stringNSString *path=@"/users/qf/desktop/my2/car.h"; Nserror*error; NSString*str6=[[nsstring alloc] Initwithcontentsoffile:path encoding:nsutf8stringencoding error:&ERROR]; NSLog (@"%@", STR6);
2. Use of strings
(1) Determine if two strings are equal and compare size (case sensitive)
NSString *str1=@"Hello World"; NSString*str2=@"Hello World"; if([str1 isequaltostring:str2]) {NSLog (@"Yes"); } Else{NSLog (@"No"); } if([str1 compare:str2]==-1) {NSLog (@"STR1<STR2"); } Else if([str1 compare:str2]==0) {NSLog (@"STR1==STR2"); } Else{NSLog (@"STR1>STR2"); }
(2) Judging comparison size (case-insensitive)
// Case insensitive if([str1 caseinsensitivecompare:str2]==-1) {NSLog (@"STR1<STR2"); } Else if([str1 caseinsensitivecompare:str2]==0) {NSLog (@"STR1==STR2"); } Else{NSLog (@"STR1>STR2"); }
(3) Case conversion
//Turn CapitalNSString *str3=[str1 uppercasestring]; NSLog (@"%@", STR3); //Turn lowercaseNSString *str4=[str2 lowercasestring]; NSLog (@"%@", STR4); //capitalize first letter, other lowercaseNSString *str5=[str2 capitalizedstring]; NSLog (@"%@", STR5);
(4) Search characters
//find if it containsNSString *str6=@"I Love Huang Xiao Dan."; NSString*str7=@" Love"; Nsrange Range=[STR6 RANGEOFSTRING:STR7]; NSLog (@"%d,%d", range.location,range.length); if(range.location==nsnotfound) {NSLog (@"didn't find"); } Else{NSLog (@"found the"); } //find a character in a location CharC=[STR6 Characteratindex:7]; NSLog (@"%c", c);
(5) Character interception
//starting from position 0, intercept to the specified position, open intervalNSString *str8=[STR6 Substringtoindex:6]; NSLog (@"%@", STR8); //starts at the specified position and is truncated to the end of the string, closing intervalNSString *STR9=[STR6 Substringfromindex:6]; NSLog (@"%@", STR9); //intercepts a specified range of stringsNsrange Range1=nsmakerange (3,5); NSString*str10=[STR6 Substringwithrange:range1]; NSLog (@"%@", STR10);
(6) Judgment begins or ends with a string
//determine if the string starts with "I" if([STR6 Hasprefix:@"I"]) {NSLog (@"Yes"); } Else{NSLog (@"No"); } //determine if the string ends with "Dan" if([STR6 Hassuffix:@"Dan"]) {NSLog (@"Yes"); } Else{NSLog (@"No"); }
(7) string to numeric type
NSString *str11=@ "ten"; NSLog (@ "%d", [Str11 intvalue]); // go to other numeric types like
(8) String substitution
NSString *str12=@ "I am a good student. " ; *str13=[str12 Stringbyreplacingcharactersinrange:nsmakerange (23) withstring:@ " Asjhja"]; NSLog (@ "%@", Str13);
(9) Segmentation and combination of strings
//Single Character SegmentationNsarray *array=[str12 componentsseparatedbystring:@" "]; NSLog (@"%@", array); //Character set for segmentationNSString *str14=@"I like a cat,and!dn sa!"; Nscharacterset*Set=[nscharacterset charactersetwithcharactersinstring:@" , !"]; Nsarray*array2=[str14 Componentsseparatedbycharactersinset:Set]; NSLog (@"%@", array2); //character CombinationsNSString *str15=[array2 componentsjoinedbystring:@"!"]; NSLog (@"%@", STR15);
Creation and use of OC Base immutable strings