NSString interception, stitching, querying, and splitting in iOS

Source: Internet
Author: User

First, the interception of NSString

1. Intercept from the beginning of the string to the specified position (not including the location)

NSString * string = @ "ABCdef";

NSString * subString1 = [string substringtoindex:3];

NSLog (@ "%@", subString1);

Output ABC

2. Starting at the end of the string specified position (including the position)

NSString * string = @ "ABCdef";

NSString * subString2 = [string substringfromindex:3];

NSLog (@ "%@", subString2);

Output def

Note: The first digit of the string has an index value of 0;

Second, the splicing of NSString

There are three ways to stitch a string

1.stringWithFormat

NSString * string1 = @ "123";

NSString * string2 = @ "456";

NSString * string3 = [NSString stringwithformat:@ "%@%@", string1, string2];

NSLog (@ "%@", string3);

Output 123456

2.stringByAppendingString

NSString * String4 = [string1 stringbyappendingstring:string2];

NSLog (@ "%@", String4);

Output 123456

3.stringByAppendingFormat

NSString * string5 = [string1 stringbyappendingformat:@ "%@", string2];

NSLog (@ "%@", string5);

Output 123456

Third, the NSString query

NSString * string = @ "ABCdef";

NSString * str = @ "abc";

1. Whether the query starts with a string

BOOL isornot = [string hasprefix:str];

2. Whether the query ends with a string

BOOL isornot = [string hassuffix:str];

3. Whether the query contains a string

Nsrange range = [string rangeofstring:str];

NSLog (@ "%ld,%ld", Range.location, Range.length); //location is where the query string is located, length is the size of the query string

Output 0, 3

Iv. Division of NSString

NSString * string = @ "Abc|def";

The Split method returns an array

Nsarray * arr = [string componentsseparatedbystring:@ "|"];

NSLog (@ "%@", arr);

Output (ABC, DEF)

Note: Little brother just opened a blog to write some essays, there is no place to hope that everyone in time, thank you.

NSString interception, stitching, querying, and splitting 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.