Eight practical NSString skills and eight nsstring skills

Source: Internet
Author: User

Eight practical NSString skills and eight nsstring skills

I wrote an article about several practical skills of NSString in iOS development. Today, we will talk about eight practical skills of NSString. You can add them to favorites to facilitate development and copy and paste them at any time.

0. uppercase letters

Code:

// Upper-case NSString * string = @ "ligang"; NSLog (@ "string: % @", [string capitalizedString]);

Print:

2015-07-16 23:06:11.652 iOSStrongDemo[10279:3062010] string: Ligang
1. Split the string

Code:

// Split the string NSString * string = @ "This is a iOSDevTip"; NSArray * array = [string componentsSeparatedByString: @ "a"]; NSString * string1 = [array objectAtIndex: 0]; NSString * string2 = [array objectAtIndex: 1]; NSLog (@ "string1: % @ string2: % @", string1, string2 );

Print:

2015-07-16 22:40:39.559 iOSStrongDemo[10165:3055448] string1:This is   string2: iOSDevTip
2. append a string

Code:

// Append the string NSMutableString * string = [[NSMutableString alloc] initWithString: @ "I Love"]; [string appendString: @ "China"]; NSLog (@ "string: % @ ", string );

Print:

2015-07-16 22:42:32.305 iOSStrongDemo[10189:3056410] string:I Love China
3. Insert a string

Code:

// Insert the string NSMutableString * string = [[NSMutableString alloc] initWithString: @ "I China"]; [string insertString: @ "Love" atIndex: 2]; NSLog (@ "string: % @", string );

Print:

2015-07-16 22:44:10.706 iOSStrongDemo[10206:3057014] string: I Love China
4. delete a string

Code:

// Delete the string NSMutableString * string = [[NSMutableString alloc] initWithString: @ "I love China"]; [string deleteCharactersInRange: NSMakeRange (2, 4)]; NSLog (@ "String1: % @", string );

Print:

2015-07-16 22:46:58.437 iOSStrongDemo[10219:3057749] String1: I  China
5. Determine whether the prefix and suffix are included

Code:

// Determine whether the prefix suffix NSString * string = @ "I love China"; BOOL isHasI = [string hasPrefix: @ "I"]; BOOL isHasChina = [string hasSuffix: @ "China"];
6. Replace the string

Code:

// Replace the string NSString * string = @ "I love China"; NSString * replaceString = [string stringByReplacingOccurrencesOfString: @ "love" withString: @ "like"]; NSLog (@ "replaceString: % @", replaceString );

Print:

2015-07-16 22:56:07.405 iOSStrongDemo[10236:3059503] replaceString:  I like China
7. Remove spaces and linefeeds at the beginning and end of the string

Code:

// Remove spaces at the beginning and end of the string and the linefeed NSString * string = @ "I love China"; NSString * text = [string character: [NSCharacterSet whitespaceAndNewlineCharacterSet]; NSLog (@ "text: % @ ", text );

Print:

2015-07-16 23:00:47.845 iOSStrongDemo[10265:3061013] text:I love China

The Code has been updated. demo: iOSStrongDemo

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.