objective-c The class in the core processing string is NSString with the nsmutablestring , the biggest difference between the two classes is NSString after the assignment is created, the content and length of the string cannot be changed dynamically unless the string is re-assigned. nsmutablestring creates an assignment to dynamically change the content and length on that string.
1. Create a classic nsstring string
NSString with the char* The big difference is NSString is a Objective Object , and char* is a byte array. The @+ " string " is the standard usage of objective-c nsstring string constants, char* No need to add @ when creating
- -(void) viewdidload
- {
- [Super Viewdidload];
- Classic string Assignment
- NSString *STR0 = @ "I am the Rain pine Momo";
- The string formatting merge includes each of the
- nsstring* type int type char* type
- NSString *str1 = [NSString stringwithformat:@ "My Name:%@ my age:%d my mailbox:%s", @ "Yu song Momo", "[email protected]"];
- String assignment parameter can only write a string and the first kind of very similar
- NSString *STR2 = [NSString stringwithstring:@ "I am the string"];
- Convert string to utf-8 format parameter to char* type
- NSString *STR3 = [NSString stringwithutf8string: "String conversion utf-8 format"];
- String merge
- int i = 100;
- char*c = "Xuanyusong";
- NSString *temp = @ "I am a temporary string";
- The base of the string temp continues to add int i with char* c to form a new string
- NSString *STR4 = [Temp stringbyappendingformat:@ "integer:%d character type:%s", I,c];
- On the basis of the string temp continue to add temp and compose a new string
- NSString *STR5 = [temp stringbyappendingstring:temp];
- String output
- NSLog (@ "STR0 =%@", STR0);
- NSLog (@ "str1 =%@", str1);
- NSLog (@ "str2 =%@", str2);
- NSLog (@ "STR3 =%@", STR3);
- NSLog (@ "STR4 =%@", STR4);
- NSLog (@ "STR5 =%@", STR5);
- }
2. traversal of strings
each string is actually made up of several Char character, the traversal of a string actually extracts every character in a string.
- -(void) viewdidload
- {
- [Super Viewdidload];
- Classic string Assignment
- NSString *str = @ "Yusongmomo";
- The length of the string
- int count = [str length];
- NSLog (@ "The length of the string is%d", count);
- Iterate through every character in a string
- for (int i =0; i < count; i++)
- {
- char c = [str characteratindex:i];
- NSLog (@ "string%d bits is%c", i,c);
- }
- }
3. Comparison of strings
isequaltostring compares strings for exact equality, not identical in case, and does not match exactly.
Hasprefixe Match String Header
Hasuffix match the tail of a string
- -(void) viewdidload
- {
- [Super Viewdidload];
- NSString *str0 = @ "Rain pine Momo";
- NSString *str1 = @ "Rain pine Momo";
- String exact equality Comparison
- if ([Str0 ISEQUALTOSTRING:STR1])
- {
- NSLog (@ "string is exactly equal");
- }
- Strings are compared at the beginning
- if ([Str0 hasprefix:@ "Rain Pine"])
- {
- NSLog (@ "String str0 begins with a rain pine");
- }
- Strings are compared at the end
- if ([str1 hassuffix:@ "MOMO"])
- {
- NSLog (@ "str1 string ends with Momo");
- }
- }
4. string interception and case change
- -(void) viewdidload
- {
- [Super Viewdidload];
- NSString *str0 = @ "Chinese name is Xuanyusong";
- Intercepts the starting point of a string to the content between index 4
- NSString * to = [STR0 substringtoindex:4];
- NSLog (@ "to =%@", to);
- Intercepts the contents of the character index from 2 to the end
- NSString * from = [STR0 substringfromindex:2];
- NSLog (@ "from =%@", from);
- Set the range of intercept strings
- From second place to tenth place
- Nsrange rang = Nsmakerange (2, 10);
- NSString * Strrang = [Str0 Substringwithrange:rang];
- NSLog (@ "rang =%@", Strrang);
- Set the string to capitalize the first letter
- NSLog (@ "STR0 First Capital:%@", [Str0 capitalizedstring]);
- Sets the entire contents of the string to uppercase
- NSLog (@ "STR0 Capital:%@", [Str0 uppercasestring]);
- Set the character to all lowercase
- NSLog (@ "Str0 lowercase:%@", [Str0 lowercasestring]);
- }
5. searching strings and replacing strings
- -(void) viewdidload
- {
- [Super Viewdidload];
- NSString *str0 = @ "Chinese name is Xuanyusong";
- NSString *temp = @ "is";
- Nsrange rang = [Str0 rangeofstring:temp];
- NSLog (@ "The string searched at the starting point in Str0 of index%d", rang.location);
- NSLog (@ "The string searched at the end of the STR0 in the index is%d", rang.location + rang.length);
- Replace a string in a search with a new string
- NSString *str = [str0 stringbyreplacingcharactersinrange:rang withstring:@ "wow ka ka Kaka"];
- NSLog (@ "replacement string is%@", str);
- Replace All "" in the string with *
- str = [STR0 stringbyreplacingoccurrencesofstring: @ "" withstring:@ "@"];
- NSLog (@ "replacement string is%@", str);
- }
Expand: Use the following method to replace the whole string and set the replacement area. ons:<# (nsstringcompareoptions) #> range:<# (nsrange) #>
Stringbyreplacingoccurrencesofstring: (NSString *) withstring: (NSString *) options: (nsstringcompareoptions) Range: ( Nsrange)
The class of core processing strings in Bjective-c is NSString and nsmutablestring