NSString
NSString is an object type that is often used in the development of an iOS OC. NSString is a subclass of NSObject (Foundation object), so it has all the attributes of NSObject.
NS is the prefix for the Cocoa class object type, derived from the name of the operating system of jobs NEXTSTEP
1. Create a NSString object
1.1 This method does not require manual free memory.
@" The IS String "; // Note: This is not the same as the C language string, you need to put an @ symbol on the head
1.2 This method requires manual memory release
Nsstring*astring =[[nsstring alloc] Initwithformat:@ "this isString"];
2. using the NSString object
NSString *string@ "This wasa person Class"; NSLog (@ "%@"string);
3. release NSString
[string
4.NSString Common Methods
NSString * Stringa =@"The is string A"; NSString* STRINGB =@"This is string B"; NSString*result; Nscomparisonresult Compareresult;//calculating characters in a stringNSLog (@"Length of Stringa:%lu", [Stringa length]);//determines whether a string containsNsrange range = [TempStr rangeofstring:@")\">"];if(range.location = = Nsnotfound) {//does not contain}Else//include{ }//copy a string to another stringresult =[NSString Stringwithstring:stringa]; NSLog (@"Copy:%@", result);//Copy a string to the end of another stringSTRINGB =[Stringa STRINGBYAPPENDINGSTRING:STRINGB]; NSLog (@"concatentation:%@", STRINGB);//Verify that two strings are equalif([Stringa isequaltostring:result] = =YES) {NSLog (@"Stringa = = Result");}Else{NSLog (@"Stringa! = result");}//verifies whether a string is less than, equal to, or greater than another stringCompareresult =[Stringa COMPARE:STRINGB];if(Compareresult <nsorderedascending) {NSLog (@"Stringa < STRINGB");}Else if(Compareresult = =nsorderedsame) {NSLog (@"Stringa = stringb");}Else{NSLog (@"Stringa > StringB");}//Convert a string to uppercaseresult =[Stringa uppercasestring]; NSLog (@"uppercasestring Conversion:%s", [result utf8string]);//Convert a string to lowercaseresult =[Stringa lowercasestring]; NSLog (@"lowercasestring Conversion:%s", result); NSLog (@"Original string:%@", Stringa);//(1) Intercept string "|http://www.baidu.com" ," | " Characters in front and back of the data, respectively outputNSString * Stringa =@"20|http://www.itheima.com"; Nsarray* STRINGB = [Stringa componentsseparatedbystring:@"|"];//use | To split strings into arrays for(StringainchSTRINGB) {//Traverse OutputNSLog (@"obj =%@", Stringa);}//(2) Remove the efgk from the string @ "Abcd_efgk" and let the uppercase letters be converted to lowercase, output efgkNSString * STR3 =@"Abcd_efgk"; Nsrange Range= {5,4};//define rang, start at 5, get 4NSString * STR4 = [STR3 substringwithrange:range];//Get EfgkNSLog (@"%@", [STR4 lowercasestring]);//output, and use lowercasestring to convert to lowercase//(3) Given a string, determine whether the string contains "PNG", if any, delete. NSString * STR5 =@"skejf_png_ksd_ppng —————— PNG"; NSString* STR6 = [STR5 stringbyreplacingoccurrencesofstring:@"PNG"Withstring:@""];//Replace PNGNSLog (@"%@", STR6);//Print//Array to stringNSLog (@"%@", [[Str7 Componentsseparatedbycharactersinset:characterset] componentsjoinedbystring:str8]);
5 nsmutablestring Common methods
//capacity capacity, specifying a high efficiency of storage space. If the string length is greater than 10, the storage space is automatically increased. nsmutablestring * Stringa = [[Nsmutablestring alloc] Initwithcapacity:Ten];//setString Setting Strings[Stringa setString:@"Apple 1234"];//appendString Stitching Strings[Stringa appendString:@"567"];//AppendFormat Stitching format string[Stringa AppendFormat:@"Age =%i",888];//Replacecharactersinrange:range Withstring:id//replacing strings based on ranges//Find RangeNsrange range = [Stringa rangeofstring:@"ple"];//Start Replace[Stringa replacecharactersinrange:range withstring:@"*"];//insertstring:< # (NSString *) #> atindex:< # (Nsuinteger) #>//inserts a string after the specified index[Str1 insertstring:@"HHH"Atindex:3];//deletecharactersinrange:< # (Nsrange) #>//Delete the specified range string[Stringa Deletecharactersinrange:range]; NSLog (@"%@", Stringa); [Stringa release];
Description Method:
The program can use NSLog to print objects, but the default is to display the class name and the address of the object in memory. And when we need to customize the print content for, we can overload the description method.
// method Implementation -(NSString *) Description { return@ "name =%@, age =%i, height =%i, wid th =%i", _name, _age, _height, _width];} // Print (U is the object created) NSLog (@ "%@", [u description]);
Dark Horse programmer _ios Development _OBJECTIVE-C Study notes _nsstring