Nsange, iosnsrange
From the network. Save it for yourself and hope it will be useful to you!
Definition of nsange
Typedef struct _ nsange
{
NSUInteger location;
NSUInteger length;
} Nsange;
Nsange is a struct, where location is an index starting with 0, and length is the length of the object. They are all NSUInteger types. The NSUInteger type is defined as follows:
# If _ LP64 _ | TARGET_ OS _EMBEDDED | TARGET_ OS _IPHONE | TARGET_ OS _WIN32 | NS_BUILD_32_LIKE_64
Typedef unsigned long NSUInteger;
# Else
Typedef unsigned int NSUInteger;
# Endif
Example:
In the following example, IPA is output.
NSString * homebrew = @ "Imperial India Pale Ale (IPA )";
// Starting at position 25, get 3 characters
Nsange range = NSMakeRange (25, 3 );
// This wowould also work:
// Nsange range = {25, 3 };
NSLog (@ "Beer shortname: % @", [homebrew substringWithRange: range]);
Search string:
NSString * homebrew = @ "Imperial India Pale Ale (IPA )";
Nsange range = [homebrew rangeOfString: @ "IPA"];
// Did we find the string "IPA "?
If (range. length> 0)
NSLog (@ "Range is: % @", NSStringFromRange (range ));
The above program will output Range is: {25, 3 }. NSStringFromRange () method, returns an nsange to an NSString. Another function, nsangefromstring (), is to convert NSString to nsange.
In the following example, we will reverse search for strings from the back to the back:
NSString * homebrew = @ "Imperial India Pale Ale (IPA )";
// Search for the "ia" starting at the end of string
Nsange range = [homebrew rangeOfString: @ "ia" options: NSBackwardsSearch];
// What did we find
If (range. length> 0)
NSLog (@ "Range is: % @", NSStringFromRange (range ));
The above program will output: Range is: {12, 2}
Ac
If you want to obtain a string or a subset of an array, using nsange will easily define this subset.
Nsange Definition
Declaration: typedef struct _ nsange {
NSUInteger location;
NSUInteger length;
} Nsange;
Create a method definition for nsange
Declaration: nsange NSMakeRange (
NSUInteger loc,
NSUInteger len
);
For example, to obtain a subset of an array:
Nsange range = NSMakeRange (0, 5 );
NSArray * subArray = [self. states subarrayWithRange: range];
In this way, the subset of the Five Elements starting from 0 in the array is obtained.
When talking about the NSString method, you will find that many of them involve the nsange method. Don't worry, it is actually a C-language structure to help describe a series of terms, including a starting position and a counting method. For example, a row or column extracts a substring from another string. You can specify the starting position and the number of required elements ).
Nsange Definition
The structure of the nsange is defined as follows:
Typedef struct_nsange
{
NSUInteger location;
NSUInteger length;
} Nsange;
Location is the starting position in the row and column (based on the zero point), and length is the number of entries in the row and column. NSUInteger is just an unsigned value. It supports 32-bit and 64-bit systems. The following is a definition of it:
# If _ LP64 _ | TARGET_ OS _EMBEDDED | TARGET_ OS _IPHONE | TARGET_ OS _WIN32 | NS_BUILD_32_LIKE_64
Typedef unsigned long NSUInteger;
# Else
Typedef unsigned int NSUInteger;
# Endif
Nsange and string
This example shows how to create a row and column. extract a substring using the same string. The output result is IPA.
NSString * homebrew = @ "ImperialIndia Pale Ale (IPA )";
// The starting position is 25 and three feature values are obtained.
Nsange range = NSMakeRange (25, 3 );
// This method is equally effective
// The columns in the nsange column are {25, 3}
NSLog (@ "Beershortname: % @", [homebrewsubstringWithRange: range]);
If you want to search for a substring, you can use the following code:
NSString * homebrew = @ "ImperialIndia Pale Ale (IPA )";
Nsange range = [homebrew rangeOfString: @ "IPA"];
// Did we find the string "IPA "? Have we found the string "IPA?
If (range. length> 0)
NSLog (@ "Range is: % @", NSStringFromRange (range ));
The output result is: Range is: {25, 3 }. One of them is very important, that is, the NSStringFromRange () Command will display the value of the returned (a row and column) as an NSString. You can also create a string row and column to obtain the nsangefromstring () function.
Let's look at another example. The following code searches for the "ia" string from the end of the string:
NSString * homebrew = @ "ImperialIndia Pale Ale (IPA )";
// Search for the "ia" string from the end
Nsange range = [homebrew rangeOfString: @ "ia" options: NSBackwardsSearch];
// Identify whether the searched string is correct
If (range. length> 0)
NSLog (@ "Range is: % @", NSStringFromRange (range ));
The above results will show: Range is: {12, 2} ("ia" appears in the word "India)
Nsange Function
The following are some functions that may be required when processing columns:
NSEqualRanges ()
NSIntersectionRange ()
NSLocationInRange ()
NSMakeRange ()
NSMaxRange ()
Nsangefromstring ()
NSStringFromRange ()
NSUnionRange ()