Ios string judgment

Source: Internet
Author: User

Ios string judgment

 
 
1. scope: [plain] view plaincopy typedef struct _ nsange {unsigned int location; unsigned int length;} nsange; // location indicates the starting position where the field stores the range, and length indicates the number of elements contained in the range. For example, in the string "Objective-C is a cool language", the word "cool" can be expressed in the range of 17 in loaction and 4 in length. Maybe because the Locatio field is not initialized, its value can be NSNotFound, which indicates the meaningless range. You can create a new nsange: [plain] view plaincopy (1) in three ways and assign the value to the field: nsange range: range. location = 17; range. length = 4; (2) apply the aggregation structure Assignment Mechanism of C to nsange range = {17, 4}; (3) Use a shortcut function NSMakeRange () provided by Cocoa (); nsange range = NSMakeRange (17,4); 2. ry data type: [plain] view plaincopy (1) NSPoint and NSSize. First, check their struct: typedef struct _ NSPoint {float x; float y;} NSPoint; typedef struct _ NSSize {float width; float height;} NSSIze; (2) rectangular NSRect typedef struct _ NSRect {NSPoint origin; NSSize size;} NSRect; (3) they also include: creating shortcuts for data types, NSMakePoint (), NSMakeSize (), and NSMakeRect (). 3. string type operation:
(1) create a string. In addition to directly assigning values to create an NSString, you can also use the stringWithFormat Method
+ (Id) stringWithFormat: (NSString *) format ,......... example: NSString * height; height = [NSString stringWithFormat: @ "Your height is % d feet, % d inches", 5, 11]; The obtained string is "Your height is 5 feet, 11inches ". there are two points to note here. The first is the ellipsis after this method, which indicates that this method can receive multiple other parameters separated by commas, the second is that the method declaration starts with a "+", which indicates creating class objects of the class. We call this method "Factory method ". (2) obtain the string size: length. The usage is as follows: unsigned int length = [height length]. This method can accurately process international strings. Such as Russian, Chinese, and Japanese. (3) NSString comparison operation: 1. -(BOOL) isw.tostring: (NSString *) aString; use the following NSString * thing1 = @ "hello 2"; NSString * thing2; thing2 = [NSString stringWithFormat: @ "hello % d", 5]; if ([thing1 isEqualToString: thing2]) {NSLog (@ "They are the same! ") ;}// Note that this method compares the pointer value of a string, and" = "compares the pointer value. 2. -(optional) compare: (NSString *) string; usage: [@ "zoinks" compare: @ "jinies"] NSOrderedDescending (indicating that the character on the left is behind the character on the right) is returned. 3. -(NSComparisonResult) compare: (NSString *) string options: (unsigned) mask; the options parameter is a bitmask. You can use bitwise OR operators to add option tags. Example: if ([thing1 compare: thing2 options: NSCaseInsensitiveSearch | NSNumericSearch] = NSOrderedSame) {NSLog (@ "They match! ");} (4) whether the string contains another string 1. -(BOOL) hasPrefix :( NSString *) aString; // check whether the string starts with another string-(BOOL) hasSuffix :( NSString *) aString; // check whether the string ends with another string-(nsange) rangeOfString: (NSString *) aString; // when rangeOfString: is sent to an NSString object, the passed parameter is the string to be searched. It returns a nsange struct to tell you where the matching part of the string is and the number of matching strings. Example: nsange range; NSString * filename = @ "draft-chapter.pages"; range = [fileName rangeOfString: @ "chapter"]; returned range. start is 6, range. length is 7. if the passed parameter is not found in the receiving string, the range. start is equivalent to NSNotFound. 5. variability. NSString is immutable, which does not mean you cannot operate on them. Cocoa provides an NSString subclass called NSMutableString (a variable string) (1)-(id) stringWithCapacity: (unsigned) capacity; // note that this capacity is only a suggestion for NSMutableString. Example: string = [NSMutableString stringWithCapcity: 42]; (2) with a variable string, you can perform various operations on it: 1. -(void) appendString: (NSString *) aString; 2. -(void) appendFormat: (NSString *) format ,......; example: NSMutableString * string; string = [NSMutableString stringWithCapacity: 50]; [string appendString: @ "Hello there"]; [string appendFormat: @ "human % d! ", 39]; The running result of this Code is string assigned" Hello there human 39 !" 3. -(void) deleteCharactersInRange: (nsange) range; deleteCharactersInRange: And rangeOfString: are often used together. Example: NSMuableString * friends; friends = [NSMutableString limit: 50]; [friends appendString: @ "JAmes BethLynn Jack Evan"]; next, locate JAck's character range: nsange jackRange; jackRange = [friends rangeOfString: @ "Jack"]; jackRange. length ++; // This sentence means that the space at the end is also counted. [Friends deleteCharactersInRange: jackRange]; Result: "James BethLynn Evan"
 

Typedef struct _ nsange {unsigned int location; unsigned int length;} nsang
 
// Location indicates the starting position where the field stores the range, and length indicates the number of elements contained in the range.


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.