Today the main practice nsstring nsmutablestring Nsarray Nsmutablearray
NSString ( string)
Method 1: [[NSString alloc] initwithformat:@ ""] creates a string object, formats it, and assigns a value to the string object
method 2:stringwithformat:@ "" structure equals [[NSString alloc]initwithformat:@ "]; convenience Builder to create a string
The 3:stringwithutf8string method converts a C string to an OC string object, which is important Note: I am a C -language string, so I do not add @ symbols
the above method is to understand NSString below we introduce the NSString of the three methods
creates a string object and assigns a value to the string object a Web site
NSString *str = [[NSString alloc] initwithformat:@ "http://item.taobao.com/item.htm?spm=a230r.1.14.83.wdbg0h &id=44327943363&ns=1&abbucket=2#detail"];
1. gets the length of the string keyword
Nsinteger length = [str length];
2. converts a string object to an uppercase keyword uppercasestring
NSString *upperstr = [str uppercasestring];
3. turns a string object into a lowercase keyword lowercasestring
NSString *lowerstr = [str lowercasestring];
4. decision string Start keyword: hasprefix
BOOL isbegin = [str hasprefix:@ "http:"];
1 means yes or TRUE
5. determine the end of string keyword Hassuffix
BOOL isend = [str hassuffix:@ "http:"];
6. stitching A string keyword: stringbyappendingstring Note: will be used frequently
NSString *appendstr = [str stringbyappendingstring:@ "/pinjie"];
7. to replace a string with a keyword: stringbyreplacingoccurrencesoffstring
Note : The replacement is a string , not a character
NSString *replacestr = [str stringbyreplacingoccurrencesofstring:@ "http" withstring:@ "MMMM"];
8. to compare keywords to a string : Compare Note: It is often used
Note : If the print result is 1, the front small if 0 means two equals if 1 indicates the front of the large
Nsinteger result = [@ "cc" compare:@ "BB"];
9. determine if a string is equal keywords: isequaltosting
BOOL isequal = [@ "AB" isequaltostring:@ "AC"];
10.1 intercept string keyword: substringfromindex
NSString *substr1 = [str substringfromindex:10];
Substringfromindex: indicates that the end of the string is truncated from the given subscript ( including the current subscript);
10.2 intercept string keyword: substringtoindex
NSString *SUBSTR2 = [str substringtoindex:25];
intercepts the first character to the 25th one
10.3 intercept string keyword: substringwithrange
There are many ways to quickly create structures in OC, starting with Nsmake + corresponding struct names, for example: (Nsmakerange ( position) ( length))
NSString *rangestr = [Str substringwithrange:nsmakerange (7, 25)];
11. gets the range of the string rangeofstring
Nsrange range = [str rangeofstring:@ "Taobao"];
NSLog (@ "%ld,%ld", range.location,range.length);
12. Convert base type to string keyword: initwithformat
NSString *intstr = [[NSString alloc] initwithformat:@ "%d", 10];
NSString *intstr = @ "ten";// Laughter grammar
for the NSString object, It is not possible to change, no matter what the above method does to NSString , does not have the table NSString itself, Instead, it changed the copy of the nsstring.
if you want to change the nsstring itself, then you need to use its subclasses nsmutablestring on the basis of the above methods, more and more deleted
nsmutablestring *strmut = [[Nsmutablestring alloc] initwithformat:@ " I am a mutable string"];
Insert Method keywords: insert
[Strmut insertstring:@ " haha" atindex:8];
NSLog (@ "%@", Strmut);
The result is : I am a mutable string haha
Stitching Method Key words: Append
[Strmut appendstring:@ "/What Are you Laughing"];
The result is : I am a mutable string haha /What are you laughing at?
Delete method keyword: delete
[Strmut Deletecharactersinrange:nsmakerange (6,7)];
Reset Method keyword: set
[Strmut setstring:@ " I have been changing"];
Nsarray (Array)
Create three string objects
NSString *str1 = [NSString stringwithformat:@ "IPhone4"];
NSString *STR2 = [[NSString alloc] initwithformat:@ "IPhone5"];
NSString *STR3 = @ "IPhone6";
NSLog (@ "%@%@%@", STR1,STR2,STR3);
Nsarray
method 1:initwithobjects because it is an array, you need to pass in multiple objects, which are separated by "," and end with nil.
Create an Array object to receive the incoming objects .
Nsarray *arr1 = [[Nsarray alloc] initwithobjects:str1,str2,str3, nil];
NSLog (@ "%@", arr1);
Method 2:objectatindex: Finding an object by subscript will only find the first conforming object , even after the first object is not displayed , after the first one is found, it returns back .
NSString *str = [arr1 objectatindex:1];
NSLog (@ "%@", str);//nsstring type IPhone5
Nsinteger index = [arr1 INDEXOFOBJECT:STR2];
NSLog (@ "%ld", index),//arr1 subscript 1
Method 3: View the number of array elements
Nsinteger count = [arr1 count];
NSLog (@ "%ld", count);// Result: There are 3 elements in arr1
Method 4: Print out individual elements by facilitating
for (int i = 0; i < Arr1.count; i++) {
NSLog (@ "%@", [arr1 objectatindex:i]);
}
Method 5: sort sortedarrayusingselector: @selector (compare:) This method is provided by the system, the interior has been sorted, so know the method is good, There is no need for undue investigation.
Nsarray *sortarray = [arr1 sortedarrayusingselector: @selector (compare:)];
NSLog (@ "%@", Sortarray);
Nsmutablearray (variable group)
Nsmutablearray inheritance and Nsarray so Nsarray method Nsmutablearray can also be used
creating a mutable Array object
Nsmutablearray *mutarray = [[Nsmutablearray alloc] initwithobjects:str1,str3, nil];
Method 1: add addobject
[Mutarray ADDOBJECT:STR1];
[Mutarray ADDOBJECT:STR2];
[Mutarray ADDOBJECT:STR3];
NSLog (@ "%@%@%@", STR1,STR2,STR3);
Method 2: Delete the Removeobjectatindex
[Mutarray removeobjectatindex:1];
[Mutarray removeobjectatindex:0];
[Mutarray Removeobjectatindex:2];
NSLog (@ "%@", Mutarray);
Method 3: swap exchangeobjectatindex:
[Mutarray exchangeobjectatindex:1 withobjectatindex:0];
NSLog (@ "%@", Mutarray);
Method 4: sort sortusingselector: @selector (compare:)
nsstring *str1 = @ "Jack";
nsstring *str2 = @ "Henry";
nsstring *STR3 = @ "Elyse";
nsstring *STR4 = @ "John";
nsstring *STR5 = @ "Justin";
Nsmutablearray *mutablearray = [[nsmutablearray alloc] INITWITHOBJECTS:STR1,STR2,STR3,STR4,STR5, Nil];
[Mutablearray Sortusingselector:@selector (compare:)];
NSLog (@ "%@", Mutablearray);
Method 5: Get the first element of the array firstobject
[Mutablearray firstobject];
NSLog (@ "%@", Mutablearray. Firstobject);
Method 5: get the last element of the array
[Mutablearray lastobject];
NSLog (@ "%@", Mutablearray. Lastobject);
}
return 0;
Methods of Objective-c NSString