Basic OC string operations and basic oc operations

Source: Internet
Author: User

Basic OC string operations and basic oc operations

The variable string modification method has a return value (re-pointing to the new string address)

The variable string modification method does not return values (modifying the string itself)

/*

NSString immutable string

*/

// 1. Create a String object

// Use the initialization method to create an object

NSString * string1 = [[NSString alloc] initWithFormat: @ "hello, world"];

 

// Use the Class Method

NSString * string2 = [NSString stringWithFormat: @ "hello, world"];

 

// Use literal

NSString * string3 = @ "hello, kitty ";

 

 

// 2. Get the string length

NSLog (@ "string1length = % ld", string1.length );

NSLog (@ "string1length = % ld", [string1 length]);

 

 

// 3. Get the characters at the specified position

Unichar ch = [string2 characterAtIndex: 3];

NSLog (@ "ch = % c", ch );

 

 

// 4. Determine the prefix and suffix of a string

NSString * string4 = @ "www.oba .png ";

BOOL isHasSuffix = [string4 hasSuffix: @ ". png"];

BOOL isHasPrefix = [string4 hasPrefix: @ "www."];

NSLog (@ "isHasSuffix = % d, isHasPrefix = % d", isHasSuffix, isHasPrefix );

 

// 5. Search for the range of a string in another string

NSString * string5 = @ "surging clouds ";

NSString * string6 = @ "";

// A struct in the specified range contains two parts.

// Location indicates the start position and length indicates the length

Nsange range = [string5 rangeOfString: string6];

NSLog (@ "location = % ld, length = % ld", range. location, range. length );

 

 

// 6. String Truncation

NSString * string7 = @ "hello world kitty ";

 

// Truncate a string within a specified range

Nsange range1 = NSMakeRange (0, 5 );

NSString * subStr1 = [string7 substringWithRange: range1];

NSLog (@ "% @", subStr1 );

 

// Capture a certain length from the beginning

NSString * subStr2 = [string7 substringToIndex: 5];

NSLog (@ "% @", subStr2 );

 

// Extract from the specified position and backward, including the subscript position of the specified position

NSString * subStr3 = [string7 substringFromIndex: string7.length-5];

NSLog (@ "% @", subStr3 );

 

 

// 7. String concatenation

NSString * string8 = @ "nana ";

NSString * string9 = @ "liuaoran ";

NSString * love = [string8 stringByAppendingString: string9];

// NSString * love1 = [NSString stringWithFormat: @ "% @", string8, string9];

NSLog (@ "love = % @", love );

 

 

// 8. String replacement

NSString * string10 = @ "Li Yifeng ";

NSString * string11 = [string10 stringByReplacingOccurrencesOfString: string10 withString: @ "Zhao lived"];

NSLog (@ "% @", string11 );

 

 

// 9. String comparison

NSString * string12 = @ "wangcai ";

NSString * string13 = @ "xiaoqiang ";

NSInteger result = [string12 compare: string13];

NSLog (@ "% ld", result );

 

// Determine whether two strings are equal

BOOL isEqual = [string12 isw.tostring: string13];

NSLog (@ "% d", isEqual );

 

 

// 10. String and numeric type conversion

NSString * string14 = @" 123 ";

NSInteger value1 = [string14 integerValue];

NSLog (@ "% ld", value1 );

 

 

// 11. String case-insensitive Conversion

NSString * string15 = @ "da feng qi xi yun fei yang ";

NSString * string16 = @ "Da Feng Qi Xi Yun Fei Yang ";

 

// Uppercase letters

NSString * str1 = [string15 capitalizedString];

NSLog (@ "% @", str1 );

 

// All uppercase letters

NSString * str2 = [string15 uppercaseString];

NSLog (@ "% @", str2 );

 

// All lowercase letters

NSString * str3 = [string16 lowercaseString];

NSLog (@ "% @", str3 );

 

 

 

/**

* NSMutableString

*/

// 1. Create an object

// Initialization Method

NSMutableString * mStr1 = [[NSMutableString alloc] initWithFormat: @ "hello, world"];

 

// Class Method

NSMutableString * mStr2 = [NSMutableString stringWithFormat: @ "hello kitty"];

 

// You cannot create a string using a literal, because all strings created using a literal are immutable.

 

 

// 2. concatenate a string

NSMutableString * mStr3 = [NSMutableString stringWithFormat: @ "Wei Zhuang"];

[MStr3 appendString: @ ""];

NSLog (@ "% @", mStr3 );

 

// 3. Insert a string

[MStr2 insertString: @ "," atIndex: 5];

NSLog (@ "% @", mStr2 );

 

 

// 4. delete a string

[MStr1 deleteCharactersInRange: NSMakeRange (5, 1)];

NSLog (@ "% @", mStr1 );

 

 

// 5. String replacement

NSMutableString * mStr4 = [NSMutableString stringWithFormat: @ "Tian Ming and Shao Yu are good buddies"];

[MStr4 replaceCharactersInRange: NSMakeRange (3, 3) withString: @ "Er"];

NSLog (@ "% @", mStr4 );

 

 

// 6. Reset the string

[MStr4 setString: @ "starsoul"];

NSLog (@ "% @", mStr4 );

Related Article

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.