NSString and Nsarray usually practice summary

Source: Internet
Author: User

/************************* String Practice ****************************/    //Create a string//1. Quickly createNSString *STR1 =@"Happyday"; //string created by 2.allocNSString *STR2 = [[NSString alloc] initwithstring:@"Happyday"]; //3.alloc created with a format characterNSString *STR3 = [[NSString alloc] Initwithformat:@"Happyday"]; //4. Class methods are created by using a format characterNSString *STR4 = [NSString stringwithstring:@"Happyday"]; //5. Compare strings If they are identicalBOOL result =[str1 ISEQUALTO:STR2]; NSLog (@"5. Compare strings in the same way:%d", result); //6. Compare string Size nsorderedascending = -1l, Nsorderedsame, nsordereddescending his three return valuesNscomparisonresult RESULT1 = [str1 compare:str2];//This is case-sensitive.Nscomparisonresult result2 = [str1 caseinsensitivecompare:str2];//This is case- insensitive//7. String lengthNsuinteger RESULT3 =[str1 length]; //8. Case ConversionNSString *STR5 = [str1 uppercasestring];//this is converted into uppercaseNSString *STR6 = [str1 lowercasestring];//this is converted to lowercase.//9. Convert to basic data type    intA = [str1 integervalue];//convert to int type    floatb = [str1 floatvalue];//convert to float type    Doublec = [str1 doublevalue];//convert to double typeBOOL d = [str1 boolvalue];//Convert to bool type//10. Splitting a stringNsarray *array1 = [str1 componentsseparatedbystring:@""];//to divide a string into an array by means of something inside @ ""//11. Accessing the specified position character//when I see the characters, I think of the things in front of Char[Str1 Characteratindex:2];//access to the location of this character in Atindex//Interception of Strings//12.1 intercept to the specified position[Str1 Substringtoindex:3];//all characters from the beginning to the third one//12.2 Intercept from the specified position to the end[Str1 Substringfromindex:3];//starting from the third, including the third, until the end .//12.3 Specify range interceptNsrange Range1 = {2,5};  [Str1 Substringwithrange:range1]; //intercept 5 characters from the beginning of the 2nd//13. Three ways to stitch stringsNSString *STR7 = [[NSString alloc] Initwithformat:@""];//1, the creation of the time when the initialization can be assigned value//A variable string is used to stitch a stringnsmutablestring *string1 = [[Nsmutablestring alloc] Initwithformat:@""]; [String1 appendString:@""];//plus a string[String1 AppendFormat:@""];//formatted string[String1 insertstring:@""Atindex:2];//join string in 2nd place//14. Finding StringsNsrange range = [str1 rangeofstring:@""];//find @ "" where the return value is a value of type Nsrange    inta1 = Range.length;//the length of his    intB1 = range.location;//his starting position;//15. Replacing Strings[Str1 stringbyreplacingcharactersinrange:range withstring:@""];//Find a location and replace with @ ""       /************************* Array Practice ****************************/    //1. Create with AllocNsarray*ARR1 = [[Nsarray alloc] Initwithobjects:@"Array", nil];//multiple arrays into whichNsarray *arra1 = @[@"Array Object"]; //2. Create by class methodNsarray *ARR2 = [Nsarray arraywithobjects:@"same as the first one", nil]; //3. Get the object labeled 0[Arr1 Objectatindex:0];//If you're looking for an object, if you want it, you want it. Index//4. Number of elements in an array    intC1 =[arr1 Count]; //5. Determine if an object is included[Arr1 Containsobject:@""];//whether the containing object contains is contain//6. Index position of the object in the array[Arr1 Indexofobject:@""];//I 'm looking for a location.//7. Concatenate strings in an array[Arr1 componentsjoinedbystring:@""];//to connect the elements of an array together//8. Last Element Object[Arr1 Lastobject];// Last//9.xcode4.4 New syntax to create an arrayNsarray *arra2 = @[@"Array Object"];//This is the new syntax .//10.xcode4.4 new syntax to access elements in an arrayarra2[0];//This is the No. 0 element of the new syntax access    /******* variable Array Practice * * * * **/    //11. Create a variable array, set 5 storage spaceNsmutablearray *muarr1 = [[Nsmutablearray alloc] Initwithobjects:@"Array", nil];//This is a mutable array of strings//12. Adding elements[Muarr1 AddObject:@"Elements"];//Add an element[Muarr1 Addobjectsfromarray:@"the array is added"];//addend groups in arrays//13. Inserting elements in the specified position[Muarr1 InsertObject:@"Elements"Atindex:2];//add an element in the second position//14. Replacing Elements[Muarr1 Replaceobjectatindex:2Withobject:@"Elements"];//Replace the second element with a @ ""//15. Swap the position of two elements[Muarr1 Exchangeobjectatindex:2Withobjectatindex:4];//swap the second exchange with the fourth one. Exchange//16. Add an element from another array to the current array. [Muarr1 Addobjectsfromarray:@"Array"];//adds an element from another array to the current array//17. Delete the element at the specified location[Muarr1 Removeobjectatindex:2];//Delete is different from string, remove//18. Delete the specified object[Muarr1 Removeobject:@"Elements"];//a specific element//19. Delete the last element[Muarr1 Removelastobject];//is the last one .//20. Delete all elements[Muarr1 removeallobjects];//that's all.//21. Use 2 loops to iterate through an array.      for(Nsinteger i =0; I < muarr1.count;i++) {NSString*p = [Muarr1 objectatindex:i];//is to put I where I need to be the first kindNSLog (@"%@", p); }    for(Nsintegeri =0; I < muarr1.count;i++) {NSString*p = Muarr1[i];//use the new method to output the element at the appropriate locationNSLog (@"%@", p); }

NSString and Nsarray usually practice summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.