Definition of nsange
TypedefStruct_ 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
TypedefUnsignedLongNsuinteger;
# Else
TypedefUnsignedIntNsuinteger;
# Endif
Example:
In the following example, IPA is output.
Nsstring* Homebrew =@ "Imperial India pale (IPA )";
// Starting at position 25, get 3 Characters
NsangeRange = nsmakerange(25,3 );
// This wowould also work:
// Nsange range = {25, 3 };
Nslog(@ "Beer shortname: % @",[Homebrew substringwithrange: range]);
Search string:
Nsstring* Homebrew =@ "Imperial India pale (IPA )";
NsangeRange =[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 (IPA )";
// Search for the "IA" starting at the end of string
NsangeRange =[HomebrewRangeofstring: @ "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:TypedefStruct_ Nsange{
Nsuinteger location;
Nsuinteger length;
}Nsange;
Create a method definition for nsange
Declaration:NsangeNsmakerange(
Nsuinteger Loc,
Nsuinteger Len
);
For example, to obtain a subset of an array:
NsangeRange = 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.
Nsange Function
The following are some functions that may be required when processing columns:
Nsequalranges ()
Nsintersectionrange ()
Nslocationinrange ()
Nsmakerange ()
Nsmaxrange ()
Nsangefromstring ()
Nsstringfromrange ()
Nsunionrange ()