Common methods of NSNumber, Nsarray and NSString in OC

Source: Internet
Author: User

Unlike the C language, there is a separate string class NSString in the Objective-c language. In C, string is composed of char (ASCLL code) characters in OC, strings are composed of Unichar (Unicode) characters nsstring, immutable strings, that is: After creation, the content and length cannot be changed nsmutablestring, Variable string, that is: After creation, the content can also be modified before using Yo-string objects to create a new string, you can use the instance method and the convenience constructor NSString common Method 1, using the instance method and the convenience constructor to create a new string 2, get the string length 3,    Get substring 4, stitch string 5, replace string 6, string equality 7, string comparison use initialization method to create nsstring *str1 = [[NSString alloc] initwithstring:@ "name"]; NSLog (@ "%@", str1);
NSString *str11 = @ "name"; NSLog (@ "%@", str11); Use instance method to create nsstring *str2 = [nsstring stringwithstring:@ "name"];
NSLog (@ "%@", str2);
NSString *str22 = @ "name";
NSLog (@ "%@", str22); Char *cstr= "hehe"; Convert c-language string to OC object NSString *STR3 = [[NSString alloc] Initwithcstring:cstr encoding:nsutf8stringencoding ];
NSLog (@ "%@", STR3);
NSString *STR4 = [NSString stringwithcstring:cstr encoding:nsutf8stringencoding];
NSLog (@ "%@", STR4); Creates a string according to the specified format nsstring *STR5 = [[NSString alloc] initwithformat:@ "%@+%d" @ "Duke", 1001];
NSLog (@ "%@", STR5);
NSString *STR6 = [NSString stringwithformat:@ "%@+%d", @ "Duke", 1001];
NSLog (@ "%@", STR6); Creates a string object based on the file contents of the specified path nsstring *STR7 = [[NSString alloc] initwithcontentsoffile:@ "/users/lanouhn/ desktop/not named. txt "encoding:nsutf8stringencoding Error:nil];
NSLog (@ "%@", STR7);
NSString *str8 = [NSString stringwithcontentsoffile:@ "/users/lanouhn/desktop/unnamed. txt" encoding:nsutf8stringencoding Error:nil];
NSLog (@ "%@", str8), the length of the string object Nsinteger long = [Str8 length];
NSLog (@ "%ld", length); Determines whether a string has a prefix string BOOL result1 = [str8 hasprefix:@ "Li"];
NSLog (@ "%@", result1? @ "YES": @ "NO"); Determines whether a string has a suffix string BOOL result2 = [str8 hassuffix:@ "Li"];
NSLog (@ "%@", RESULT2 @ "YES": @ "NO"), determine if two strings are the same BOOL RESULT3 = [Str8 ISEQUALTOSTRING:STR7];
NSLog (@ "%@", RESULT3 @ "YES": @ "NO"), string comparison sort result Nscomparisonresult Result4 = [Str8 COMPARE:STR7];
NSLog (@ "%ld", RESULT4);//ascending to 1, descending to 1, same as 0 gets substring from the specified subscript (containing the specified subscript) to the end of the string nsstring *substr1 = [Str8 substringfromindex : 2];
NSLog (@ "%@", SUBSTR1), substring from the beginning of the string to the specified subscript character (without the specified subscript) nsstring *substr2 = [Str8 substringtoindex:2];
NSLog (@ "%@", SUBSTR2); Nsrange is the struct type, the member location describes the subscript position, and the member length describes the length of the substring that needs to be intercepted nsrange range = Nsmakerange (1, 3);
Nsrange range = {1,3};
NSString *SUBSTR3 = [Str8 Substringwithrange:range];
NSLog (@ "%@", SUBSTR3);
string concatenation generates a new string based on the given parameter string, without changing the original string nsstring *newstring1 = [str8 stringbyappendingstring:@ "fork 1001"];
NSLog (@ "%@", newString1); Creates a new string based on the specified format string and parameters nsstring *newstring2 = [str8 stringbyappendingformat:@ "%d", 1001];
NSLog (@ "%@", newString2), path stitching nsstring *newstring3 = [str8 stringbyappendingpathcomponent:@ "Xx.avi"];
NSLog (@ "%@", newString3); Substitution of strings replaces the string currently present in Str8 with a given second string nsstring *newstring4 = [str8    stringbyreplacingoccurrencesofstring:@ "Li X" withstring:@ "Matchless"; NSLog (@ "%@", NewString4); find string NSString *link = @ "Abdjofepok = _NIEIFN";
Nsrange range1 = [link rangeofstring:@ "Pok = _nie"];
NSLog (@ "%@", Nsstringfromrange (Range1));
if (range1.location! = nsnotfound) {
NSLog (@ "founded");
} The conversion of the string to the numeric class data NSString *numstring1 = @ "1";
Nsinteger IntegerValue = [NumString1 integervalue];
NSLog (@ "%ld", IntegerValue), case conversion NSString *string = @ "I love you"; turn to uppercase NSString *uppercasestr = [string uppercasestr ING];
NSLog (@ "%@", uppercasestr); turn to lowercase string nsstring *lowcasestr= [Uppercasestr lowercasestring];
NSLog (@ "%@", lowcasestr); Turns into an uppercase string nsstring *capitalstring = [string capitalizedstring]; NSLog (@ "%@", capitalstring); Nsmutablestring (mutable string) nsmutablestring is a subclass of NSString, and the string created by Nsmutablestring is a dynamic, mutable string. The commonly used methods for adding and removing strings are to create a new string splicing string insert character Delete character nsmutablestring *mutablestr1 = [[Nsmutablestring alloc] init];
NSLog (@ "%@", MUTABLESTR1); nsmutablestring *mutablestr2 = [nsmutablestring string]; splicing of variable strings stringbyappendingstring [mutableStr1 appendstring:@] Abcdeg "];
NSLog (@ "%@", MUTABLESTR1);
NSString *resultstring = [mutableStr1 stringbyappendingstring:@ "xxxxx"];
NSLog (@ "%@", MUTABLESTR1); NSLog (@ "%@", resultstring); This string concatenation, does not change the original object another string stitching method Stringbyappendingformat [mutableStr2 appendformat:@ "Duke +%d" , 1001];
NSLog (@ "%@", MUTABLESTR2); Delete string [mutableStr2 Deletecharactersinrange:nsmakerange (4,6)];
NSLog (@ "%@", MUTABLESTR2); Insert string Insert a new string before the given subscript [mutableStr2 insertstring:@ "Heheh" atindex:0];
NSLog (@ "%@", MUTABLESTR2);    The replacement string replaces the specified range of character gates with the given string [MutableStr2 replacecharactersinrange:nsmakerange (0, 5) withstring:@ "hehe"];  NSLog (@ "%@", MUTABLESTR2); The following is an example of an immutable character method and a variable method to solve a given picture file name, determine whether the string is "PNG" end, if it is replaced by "JPG", if not, splicing ". jpg". Immutable string
NSString *picname = [NSString stringwithformat:@ "Image.png"];
NSString *resultstr = nil;
if ([Picname hassuffix:@ "PNG"]) {
ResultStr = [Picname stringbyreplacingoccurrencesofstring:@ "png" withstring:@ "jpg"];
} else {
ResultStr = [Picname stringbyappendingstring:@ ". jpg"];
}
NSLog (@ "%@", resultstr);

Variable string
nsmutablestring *picture = [nsmutablestring stringwithstring:picname];
if ([Picture hassuffix:@ "PNG"]) {
[Picture replacecharactersinrange:[picture rangeofstring:@ "png"] withstring:@ "jpg"];
} else {
[Picture appendstring:@ ". jpg"];  } NSLog (@ "%@", picture); The container class that holds the data in OC is called a set (collection) array, which can only hold the concept of Subscript (index) of an object array, index the element, Subscript starting from 0 The number of component immutable groups (Nsarray) and variable arrays (Nsmutablearray) is commonly used to create an array object, use an instance initialization, or facilitate the constructor to get the number of elements based on index gets the object//definition Nsarray
Nsarray *array1 = [[Nsarray alloc] initwithobjects:@ "1", @2,@ "haha", nil];
NSLog (@ "%@", [array1 description]);
Nsarray *array2 = [Nsarray arraywithobjects:@ "1", @2,@ "?", nil];
NSLog (@ "%@", array2);
The syntax of the array of sugar forms (literal, literal)
Nsarray *array3 = @[@ "1", @2,@ "?"];
NSLog (@ "%@", array3);
Gets the number of array elements
Nsinteger count = [array3 count];
NSLog (@ "%ld", count);
Get the corresponding object by subscript
for (int i = 0; i < [array3 count]; i++) {
NSLog (@ "%@", [Array3 objectatindex:i]);
NSLog (@ "%@", Array3[i]);
}
The object to find his subscript in the array
Nsinteger index = [array3 indexofobject:@2];
NSLog (@ "%ld", index);
NSLog (@ "----------------------------------");
NSString *textstring = [NSString stringwithcontentsoffile:@ "/users/duke/desktop/unnamed. txt" encoding: Nsutf8stringencoding Error:nil];
NSLog (@ "%@", textstring);
Intercepts the original string into multiple substrings by the given string and saves it in the array.
Nsarray *array4 = [textstring componentsseparatedbystring:@ "\ n"];
NSLog (@ "%@", array4);//variable array using--------------------------------------------------------variable array is a subclass of Nsarray, All of the methods that inherit Nsarray can be used to add and delete arrays, such as adding elements to an array object, inserting elements, deleting elements, replacing elements, exchanging two elements at a specified position nsmutablearray *mutablearray1 = [[ Nsmutablearray alloc] initwitharray:array1];
NSLog (@ "%@", mutablearray1);
Nsmutablearray *mutablearray2 = [Nsmutablearray arraywitharray:array1];
NSLog (@ "%@", mutableArray2);
adding elements
[MutableArray2 addobject:@33];
NSLog (@ "%@", mutableArray2);
inserting elements
[MutableArray2 insertobject:@123 Atindex:2];
NSLog (@ "%@", mutableArray2);
Replace an existing element
[MutableArray2 replaceobjectatindex:2 withobject:@ "Heihei"];
NSLog (@ "%@", mutableArray2);
Swap the position of two objects corresponding to the subscript
[MutableArray2 Exchangeobjectatindex:2 withobjectatindex:0];
NSLog (@ "%@", mutableArray2);
Delete last Object
[MutableArray2 Removelastobject];
NSLog (@ "%@", mutableArray2);
Delete the specified element
[MutableArray2 removeobject:@2];
NSLog (@ "%@", mutableArray2);
Delete the object that specifies the subscript
[MutableArray2 removeobjectatindex:0];
NSLog (@ "%@", mutableArray2);
Delete multiple content
Delete all objects in the array
[MutableArray2 removeallobjects];
NSLog (@ "%@", mutableArray2);
Iterating through an array
Nsarray *array = [Nsarray arraywithobjects:@ "one", @ "one", @ "three", @ "four", nil];
for (int index = 0; index < [array count]; index++) {
NSString *string = [array Objectatindex:index];
NSLog (@ "%@", string);
}
NSLog (@ "-----------------------");
For (NSString *string in array) {
NSLog (@ "%@", string);
}

Common methods of NSNumber, Nsarray and NSString in OC

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.