Nsrange class in iOS

Source: Internet
Author: User

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

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.