Common Methods of NSNumber, NSArray, and NSString in OC, nsnumbernsarray

Source: Internet
Author: User

Common Methods of NSNumber, NSArray, and NSString in OC, nsnumbernsarray
Unlike C, Objective-C has a separate string class NSString. In C language, string is composed of char (ASCLL code) characters in OC, and the string is composed of unichar (Unicode) characters in NSString, immutable string, that is: after the creation, the content and length cannot be changed to NSMutableString or a variable string. That is, after the content is created, you can also modify the content. Before using a String object, you must first create a new string, you can use the instance method and convenience constructor NSString. 1. Use the instance method and convenience constructor to create a new string. 2. Obtain the string length. 3. Obtain the substring. 4. splice the string. 5. Replace string 6, character string equal 7. Use the initialization method to create NSString * str1 = [[NSString alloc] initWithString: @ "name"]; NSLog (@ "% @", str1 );
NSString * str11 = @ "name"; NSLog (@ "% @", str11); Use the instance method to create NSString * str2 = [NSString stringWithString: @ "name"];
NSLog (@ "% @", str2 );
NSString * str22 = @ "name ";
NSLog (@ "% @", str22); char * cStr = "hehe"; convert the C language string to the object NSString * str3 = [[NSString alloc] initWithCString: cStr encoding: NSUTF8StringEncoding];
NSLog (@ "% @", str3 );
NSString * str4 = [NSString stringWithCString: cStr encoding: NSUTF8StringEncoding];
NSLog (@ "% @", str4); Create a string NSString * str5 = [[NSString alloc] initWithFormat: @ "% @ + % d ", @ "duke", 1001];
NSLog (@ "% @", str5 );
NSString * str6 = [NSString stringWithFormat: @ "% @ + % d", @ "duke", 1001];
NSLog (@ "% @", str6); Create a String object NSString * str7 = [[NSString alloc] initWithContentsOfFile: @ "/Users/lanouhn/Desktop/unnamed .txt" encoding: NSUTF8StringEncoding error: nil];
NSLog (@ "% @", str7 );
NSString * str8 = [NSString stringWithContentsOfFile: @ "/Users/lanouhn/Desktop/unnamed .txt" encoding: NSUTF8StringEncoding error: nil];
NSLog (@ "% @", str8); Evaluate the length of the string object NSInteger length = [str8 length];
NSLog (@ "% ld", length); determines whether a string has a prefix of the 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"); determines whether two strings are the same BOOL result3 = [str8 isw.tostring: str7];
NSLog (@ "% @", result3? @ "YES": @ "NO"); string comparison sorting result NSComparisonResult result4 = [str8 compare: str7];
NSLog (@ "% ld", result4); // The Sub-string is obtained from the specified subscript (including the specified subscript) in ascending order of-1 and 1 in descending order) NSString * subStr1 = [str8 substringFromIndex: 2];
NSLog (@ "% @", subStr1); NSString * subStr2 = [str8 substringToIndex: 2] from the start of the string to the specified character (excluding the specified subscript);
NSLog (@ "% @", subStr2); nsange is a struct type. The location of a Member describes the subscript position. The length of a Member describes the length of the substring that needs to be intercepted. nsange range = NSMakeRange (1, 3 );
// Nsange 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: @ "Cross 1001"];
NSLog (@ "% @", newString1); concatenates a new NSString * newString2 = [str8 stringByAppendingFormat: @ "% d ", 1001];
NSLog (@ "% @", newString2); Path concatenation NSString * newString3 = [str8 stringByAppendingPathComponent: @ "xx. avi"];
NSLog (@ "% @", newString3); string replacement replaces the existing string NSString * newString4 = [str8 stringByReplacingOccurrencesOfString: @ "Li X" withString: @ ""]; NSLog (@ "% @", newString4); search for the string NSString * link = @ "abdjofepok = _ niefn ";
Nsange range1 = [link rangeOfString: @ "pok = _ nie"];
NSLog (@ "% @", NSStringFromRange (range1 ));
If (range1.location! = NSNotFound ){
NSLog (@ "founded ");
} NSString * numString1 = @ "1 ";
NSInteger integerValue = [numString1 integerValue];
NSLog (@ "% ld", integerValue); case-insensitive NSString * string = @ "I love you"; converts it to an upper-case NSString * upperCaseStr = [string uppercaseString];
NSLog (@ "% @", upperCaseStr); convert it to a lowercase string NSString * lowCaseStr = [upperCaseStr lowercaseString];
NSLog (@ "% @", lowCaseStr); converts it to an uppercase string NSString * capitalString = [string capitalizedString]; NSLog (@ "% @", capitalString); NSMutableString (variable string) NSMutableString is a subclass of NSString. The string created using NSMutableString is a dynamic and variable string. Common Operations such as adding, deleting, and modifying strings include: create a New String concatenation string insert character delete character NSMutableString * mutableStr1 = [[NSMutableString alloc] init];
NSLog (@ "% @", mutableStr1); NSMutableString * mutableStr2 = [NSMutableString string]; concatenation 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 concatenation method of another string of the original object, stringByAppendingFormat [mutableStr2 appendFormat: @ "duke + % d", 1001];
NSLog (@ "% @", mutableStr2); Delete the string [mutableStr2 deleteCharactersInRange: NSMakeRange (4, 6)];
NSLog (@ "% @", mutableStr2); Insert a new string before the given subscript [mutableStr2 insertString: @ "heheh" atIndex: 0];
NSLog (@ "% @", mutableStr2); replace string with the character door of the specified range based on the given string [mutableStr2 replaceCharactersInRange: NSMakeRange (0, 5) withString: @ "hehe"]; NSLog (@ "% @", mutableStr2); the following example uses the immutable character method and the variable method to answer a given image file name, determine whether the string ends with "png". If yes, replace it with "jpg". If not, splice it with "".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 collection array in OC is an ordered collection, the object array can only be stored with the index concept. index is used to index elements. The subscript starts from 0 and the array is divided into NSArray and NSMutableArray) the common method is to create an array object and use the instance initialization or constructor to obtain the number of elements and get the object according to the index // define NSArray
NSArray * array1 = [[NSArray alloc] initWithObjects: @ "1", @ 2, @ "Haha", nil];
NSLog (@ "% @", [array1 description]);
NSArray * array2 = [NSArray arrayWithObjects: @ "1", @ 2, @ "", nil];
NSLog (@ "% @", array2 );
// Array syntax sugar form (literal, literal)
NSArray * array3 = @ [@ "1", @ 2, @ ""];
NSLog (@ "% @", array3 );
// Obtain the number of array elements
NSInteger count = [array3 count];
NSLog (@ "% ld", count );
// Obtain the corresponding object through subscript
For (int I = 0; I <[array3 count]; I ++ ){
// NSLog (@ "% @", [array3 objectAtIndex: I]);
NSLog (@ "% @", array3 [I]);
}
// Search for his subscript in the array through the object
NSInteger index = [array3 indexOfObject: @ 2];
NSLog (@ "% ld", index );
NSLog (@"----------------------------------");
NSString * textString = [NSString stringWithContentsOfFile: @ "/Users/Duke/Desktop/unnamed .txt" encoding: NSUTF8StringEncoding error: nil];
NSLog (@ "% @", textString );
// Truncate the original string into multiple sub-strings and save them in the array.
NSArray * array4 = [textString componentsSeparatedByString: @ "\ n"];
NSLog (@ "% @", array4); // use a mutable array -------------------------------------------------------- a mutable array is a subclass of NSArray, all methods inherited from NSArray can be used to add, delete, modify, and perform other operations on the array. Common methods include: create an array object to add an element, insert an element to delete an element, and replace the two elements NSMutableArray * mutablearray1 = [[NSMutableArray alloc] initWithArray: array1];
NSLog (@ "% @", mutablearray1 );
NSMutableArray * mutableArray2 = [NSMutableArray arrayWithArray: array1];
NSLog (@ "% @", mutableArray2 );
// Add Element
[MutableArray2 addObject: @ 33];
NSLog (@ "% @", mutableArray2 );
// Insert element
[MutableArray2 insertObject: @ 123 atIndex: 2];
NSLog (@ "% @", mutableArray2 );
// Replace an existing element
[MutableArray2 replaceObjectAtIndex: 2 withObject: @ "heihei"];
NSLog (@ "% @", mutableArray2 );
// Swap the location of two objects corresponding to the underlying object
[MutableArray2 exchangeObjectAtIndex: 2 withObjectAtIndex: 0];
NSLog (@ "% @", mutableArray2 );
// Delete the last object
[MutableArray2 removeLastObject];
NSLog (@ "% @", mutableArray2 );
// Delete a specified Element
[MutableArray2 removeObject: @ 2];
NSLog (@ "% @", mutableArray2 );
// Delete the specified object
[MutableArray2 removeObjectAtIndex: 0];
NSLog (@ "% @", mutableArray2 );
// Delete multiple Contents
// Delete all objects in the array
[MutableArray2 removeAllObjects];
NSLog (@ "% @", mutableArray2 );
// Traverse the Array
NSArray * array = [NSArray arrayWithObjects: @ "one", @ "two", @ "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 );
}

Related Article

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.