OBJ-C array and String concatenation and Segmentation

Source: Internet
Author: User

OBJ-C only adds a "special corpus" C language, so you can use printf () instead of nslog (). However, we recommend that you use nslog because it adds features such as timestamp, date stamp, and automatic line break ('\ n.

 

1. The OC array members are arbitrary object pointers. Similar to the linked list structure in C (ending with nil), all Array Operations cannot cross-border.

The array of OC is divided into immutable array nsarray variable array nsmutablearray

  1. Nsarray * array = [[nsarray alloc] initwithobjects: @ "one", @ "two", @ "three", nil];
  2. Nslog (@ "% @", [array objectatindex: 0]); // obtain array members by subscript (subscript starts from 0)
  3. Nslog (@ "% lD", [array count]); // obtain the number of valid members of the array (excluding nil)
  4. For (I = 0; I <[array count]; I ++) {// traverses the Array (C method)
  5. Nslog (@ "% @", [array objectatindex: I]);
  6. }
  7. Nslog (@ "% @", array); // (OC) view the array content (send the description message to array first, and then send a description message to each member)
  8. Nsarray * array1 = [[nsarray alloc] initwitharray: array]; // creating an array object is equivalent to copying
  9. Nsarray * arry2 = [nsarray arraywitharray: array]; // creating an array object is equivalent to copying
  10. Nsenumerator * enumer = [array objectenumerator]; // Positive Sequence enumerator
  11. // Create an enumerator to assign the address of each element of the array to the enumerator once, and then establish an association (the enumerator can only be used to read group members)
  12. // When enumeration is performed, it creates an association with the array (modifying the monitor/iterator) to restrict enumeration, And the element cannot be modified. It can only be read or written.
  13. Id OBJ;
  14. // In the first loop, the first element of the array is assigned to OBJ. In the second loop, the second array element is assigned to OBJ.
  15. While (OBJ = [enumer nextobject]) {// traverses the Array
  16. Nslog (@ "% @", OBJ );
  17. }
  18. // Fast Enumeration The first loop assigns the first element of array to OB the second loop and assigns the second element of array to OB until nil (read only and cannot be changed)
  19. For (ID ob in array ){
  20. Nslog (@ "% @", ob );
  21. }
  22. // Output an array in reverse order (create an enumerator in reverse order and assign the last element to OBJ In the first loop)
  23. Nsenumerator * enumer1 = [array reverseobjectenumerator];
  24. While (OBJ = [enumer1 nextobject]) {
  25. Nslog (@ "% @", OBJ );
  26. }
  27. If ([OBJ iskindofclass: [Dog class]) // obtain the class type to determine whether it is the object of the specified class
  28. If ([OBJ ismemberofclass: [Dog class]) {
  29. }

2. Immutable Array

  1. ------- Search
  2. Nsarray * array = [[nsarray alloc] initwithobjects: @ "one", @ "two", @ "three", @ "one", nil];
  3. Nsuinteger Index = [array indexofobject: @ "one123"]; // returns nsnotfound if the subscript corresponding to the first found array member is not found
  4. Index = [array indexofobject: @ "one" inrange: nsmakerange (1, 3)]; // search within the specified range
  5. If (index! = Nsnotfound ){
  6. Nslog (@ "% lD", index );
  7. }
  8. ---- Extract to form a new array
  9. Nsarray * array1 = [array objectsatindexes: [nsindexset indexsetwithindexesinrange: nsmakerange (1, 3)];
  10. NUMBER SET
  11. Nsindexset this is a digital collection class
  12. [Nsindexset indexsetwithindexesinrange: nsmakerange (1, 3)] generates a set of numbers

3. Variable

  1. Nsmutablearray * array = [[nsmutablearray alloc] initwithobjects: @ "one", @ "two", @ "three", @ "four", nil];
  2. [Array addobject: @ "five"]; // insert an element at the end of the array
  3. [Array insertobject: @ "six" atindex: 5]; // insert an element in the specified subscript position of the array (the maximum value is length)
  4. [Array removeobject: @ "six"]; // delete a specified Element (all positions in the array will be deleted)
  5. [Array removeobject: @ "two" inrange: nsmakerange (0, 3)]; // delete a specified element within the specified length starting from the specified subscript position
  6. [Array removelastobject]; // Delete the last element
  7. [Array removeallobjects]; // deletes all elements.
  8. [Array replaceobjectatindex: 3 withobject: @ "iOS"]; // Replace the specified subscript position element with the specified element.
  9. [Array exchangeobjectatindex: 0 withobjectatindex: 3]; // exchange two elements of the specified subobject

4. string segmentation and concatenation

  1. // @ "" Empty string object
  2. ------- Segmentation
  3. Nsstring * PTR = @ "I Am a man ";
  4. Nsarray * array = [PTR componentsseparatedbystring: @ ""]; // returns the nsarray immutable array as the delimiter.
  5. Nsmutablearray * array1 = [nsmutablearray arraywitharray: array]; // if modified, nsarray is converted to nsmutablearray.
  6. Nsarray * array2 = [PTR componentsseparatedbycharactersinset: [nscharacterset charactersetwithcharactersinstring: @ ","]; // use the characters in the string as the condition for segmentation
  7. Character Set combination
  8. Nscharacterset this is a character set class.
  9. [Nscharacterset charactersetwithcharactersinstring: @ ","] // converts a string to a character set combination
  10. ------- Splicing
  11. Nsstring * STR = [array componentsjoinedbystring: @ ""];
  12. If the split condition appears at the beginning or end, an empty string @ "will appear. If not, convert it to nsmutablestring to process the empty string.
  13. Func1: [array1 removeobject: @ ""]; // directly delete an empty string
  14. Func2: For (id obj in array1 ){
  15. If ([OBJ length] = 0) // The length of the empty string is 0.
  16. If ([OBJ isequaltostring: @ ""]) // compare it with an empty string (the string cannot be =). Use a function)
  17. }

 

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.