NsranGE 's definition
typedef struct _NSRANGE
{
Nsuinteger location;
Nsuinteger length;
} Nsrange;
Nsrange is a struct in which the location is an index that starts at 0, and length is a representation of how long the object is. They are all types of Nsuinteger . 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:
The following example will output the IPA
NSString *homebrew = @ "Imperial India Pale Ale (IPA)";
Starting at position, get 3 Characters
NSRange Range = NSMakerange (3);
This wouLD also work:
Nsrange range = {3};
NSLog (@ "Beer shortname:%@", [homebrew substringwithrange:range]);
Search string:
NSString *homebrew = @ "Imperial India Pale Ale (IPa)";
Nsrange 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: {3}. Nsstringfromrange () method to return a nsrange to a nsstring. Another function, nsrangefromstring (), is to convert nsstring to Nsrange
The following example will reverse the search string from backward forward:
NSString *homebrew = @ "Imperial India Pale Ale (IPA)";
Search for the ' IA ' starting at the end of String
Nsrange range = [Homebrew Rangeofstring:@ "ia" options:nsbackwArdssearch];
What did we find
if (RAnge.length > 0)
NSLog (@ "range is:%@", Nsstringfromrange (range));
The above program will be output :Range is: {2} ("IA" appears in the word "India" )
Ac
If you want to get a string or a subset of an array, it is convenient to use Nsrange to define the subset.
Nsrange Definition
Declaration:typedef struct _nsrange {
Nsuinteger location;
Nsuinteger length;
} Nsrange;
To create a method definition for Nsrange
Declaration:nsrange Nsmakerange (
Nsuinteger Loc,
Nsuinteger Len
);
For example, get a subset of an array:
Nsrange range = NSMakeRAnge (0, 5);
Nsarray *subarray = [self.states subarraywithrange:range];
This gives you a subset of the 5 elements that begin with 0 in this array .
Find a common learning iOS development friends, can add me v txs8882909: note iOS is OK
Nsrange class in iOS