Use of nsange

Source: Internet
Author: User

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 ()

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.