Objective-C syntax string NSString

Source: Internet
Author: User

The core class for processing strings in Objective-C is NSString and NSMutableString. These two classes process most of the functions of strings in Objective-C. The main difference between the two classes is that NSString cannot dynamically modify the length and content after being assigned a value, unless it is assigned a value again. NSMutableString is similar to the linked list. After creating a value assignment, you can modify the length, insert, delete, and so on.

Note: The following code runs in XCode 4.3.2 and IOS 5.1.

NSString provides many methods to create and Initialize New strings. The following describes how to create a constant string. It is an instance of NSString compiled into an executable file and will not disappear in the memory. Example:

1. Create a constant string.

NSString * astring = @ "I Am a string ";

2. Memory Address of NSString

When we declare a string, NSString * aString;
The variable aString does not really contain a String object. It is a pointer to a string object in memory. We call it an object identifier because it represents an object in memory rather than saving this object, the pointer is the address of the memory location. We will use "=" to compare strings. Let's look at an example.

NSString * string1 = @ "I am ";

NSString * string2 = @ "I am ";

BOOL result = string1 = string2;

If (result ){

NSLog (@ "address: % p", string1 );

NSLog (@ "address: % p", string2 );

}

Run the print result:

09:36:41. 337 objectiveC [613: 403] address: 0x10485b478

09:36:41. 339 objectiveC [613: 403] address: 0x10485b478

The result value is YES. The memory address is compared here. @ "I am a-1" is stored in the text constant area. Both string1 and string2 point to this string, so it points to the same address.

If you do this
NSString * string1 = @ "I am ";
NSString * string2 = string1;
BOOL result = string1 = string2;

At this time, the result value is YES.

Let's take a look at the example. The following is an example of the string memory address.

// The Memory points to NSString * bstring = @ "I Am a string"; NSString * astring = [[NSString alloc] init]; NSLog (@ "astring point address: 0x %. 8x ", astring); NSLog (@" bstring point address: 0x %. 8x ", bstring); astring = @" I Am a string "; NSLog (@" astring point address: 0x % after the value is re-assigned. 8x ", astring); NSLog (@" astring: % @ ", astring );

The output is as follows:

11:35:31. 226 NSString [2316: f803] astring point address: 0x00b72a7c2012-06-14 11:35:31. 227 NSString [2316: f803] bstring point address: 0x000045bc2012-06-14 11:35:31. 227 NSString [2316: f803] astring point address: 0x000045bc2012-06-14 11:35:31 after the value is reassigned. 227 NSString [2316: f803] astring: I am a string

After the value is re-assigned, the astring address is the same as the bstring address.

3. Comparison of NSString strings

In NSString, the most direct method is to use isEqualTostring.

NSString * string1 = @ "I am a"; NSString * string2 = @ "I am a"; BOOL result = [string1 isw.tostring: string2]; if (result) {NSLog (@ "same string ");}

Print results: 11:49:10. 442 NSString [2481: f803] the same string

4. string segmentation:

NSString * nstring = @ "USA, Canada, Australia, Zimbabwe, Egypt"; NSArray * array = [nstring componentsSeparatedByString: @ ","]; for (int I = 0; I <[array count]; I ++) {NSLog (@ "string: % @", [array objectAtIndex: I]);}

Result:

11:49:10. 443 NSString [2481: f803] string: 11:49:10. 443 NSString [2481: f803] string: Canada 11:49:10. 443 NSString [2481: f803] string: Australia 11:49:10. 443 NSString [2481: f803] string: Zimbabwe 11:49:10. 444 NSString [2481: f803] string: Egypt

5. Create a formatted string

Int a = 100; int B = 8; NSString * astring = [[NSString alloc] initWithString: [NSString stringWithFormat: @ "% d. this is the % I string ", a, B]; NSLog (@" astring: % @ ", astring );

Format output symbols

% @ Object % d, % I integer % u unsigned integer % f floating point/double character % x, % X binary integer % o octal integer % zu size_t % p pointer % e floating point/double character (Scientific Computing) % g floating point/double character % s C string %. * s Pascal string % c character % C unichar % lld 64-bit long integer (long) % llu no character 64-bit long integer % Lf 64-bit double character % e is a real number, calculated by scientific notation

6. Use Standard c to create a string: initwithuf8string Method

Char * Cstring = "I Am a string"; NSString * astring = [[NSString alloc] initwithuf8string: Cstring]; NSLog (@ "astring: % @", astring );

Result:13:47:23. 956 NSString [2850: f803] astring:I am a string

7. Search for and replace substrings in a string

HasPrefixe matches string Headers
HaSuffix matches the string's tail

NSString * string1 = @ ""; NSString * string2 = @ ""; // The string starts with if ([string1 hasPrefix: @ "kara"]) {NSLog (@ "string string1 starting with kara");} // string end with comparison if ([string2 hasSuffix: @ "kara"]) {NSLog (@ "string2 string ends with kara ");}

Search and replace

NSString * string = @ "We Are the successor of gong's production"; NSString * temp = @ "gong"; NSString * me = @ "I"; nsange rang = [string rangeOfString: temp]; nsange rang1 = [string rangeOfString: me]; NSLog (@ "index of the start point of the searched string in string is % d", rang. location); NSLog (@ "the index of the string ending point in string is % d", rang. location + rang. length); NSLog (@ "the index of the start point in the string is % d", rang1.location ); // Replace the string in the search with a new string NSString * str = [string stringByReplacingCharactersInRange: rang withString: @ ""]; NSLog (@ "Replace the string with % @", str); // Replace "" in the string with * str = [string stringByReplacingOccurrencesOfString: @ "" withString: @ "*"]; NSLog (@ "after replacement, the string is % @", str );
Result:
14:07:44. 762 NSString [3107: f803] the index of the start point of the string to be searched is 42012-06-14 14:07:44. 764 NSString [3107: f803] the index of the end point of the string to be searched is 92012-06-14 14:07:44. 764 NSString [3107: f803] the index of the start point of my string is 02012-06-14 14:07:44. 765 NSString [3107: f803] After replacement, the string is 14:07:44 for us as the successor of the great productive ideology. 765 NSString [3107: f803] After replacement, the string is the successor of ** gong production **.

Have you seen the index of "I? The first index value is 0.

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.