The class of core processing strings in Bjective-c is NSString and nsmutablestring

Source: Internet
Author: User

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

  1. -(void) viewdidload
  2. {
  3. [Super Viewdidload];
  4. Classic string Assignment
  5. NSString *STR0 = @ "I am the Rain pine Momo";
  6. The string formatting merge includes each of the
  7. nsstring* type int type char* type
  8. NSString *str1 = [NSString stringwithformat:@ "My Name:%@ my age:%d my mailbox:%s", @ "Yu song Momo", "[email protected]"];
  9. String assignment parameter can only write a string and the first kind of very similar
  10. NSString *STR2 = [NSString stringwithstring:@ "I am the string"];
  11. Convert string to utf-8 format parameter to char* type
  12. NSString *STR3 = [NSString stringwithutf8string: "String conversion utf-8 format"];
  13. String merge
  14. int i = 100;
  15. char*c = "Xuanyusong";
  16. NSString *temp = @ "I am a temporary string";
  17. The base of the string temp continues to add int i with char* c to form a new string
  18. NSString *STR4 = [Temp stringbyappendingformat:@ "integer:%d character type:%s", I,c];
  19. On the basis of the string temp continue to add temp and compose a new string
  20. NSString *STR5 = [temp stringbyappendingstring:temp];
  21. String output
  22. NSLog (@ "STR0 =%@", STR0);
  23. NSLog (@ "str1 =%@", str1);
  24. NSLog (@ "str2 =%@", str2);
  25. NSLog (@ "STR3 =%@", STR3);
  26. NSLog (@ "STR4 =%@", STR4);
  27. NSLog (@ "STR5 =%@", STR5);
  28. }

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.

  1. -(void) viewdidload
  2. {
  3. [Super Viewdidload];
  4. Classic string Assignment
  5. NSString *str = @ "Yusongmomo";
  6. The length of the string
  7. int count = [str length];
  8. NSLog (@ "The length of the string is%d", count);
  9. Iterate through every character in a string
  10. for (int i =0; i < count; i++)
  11. {
  12. char c = [str characteratindex:i];
  13. NSLog (@ "string%d bits is%c", i,c);
  14. }
  15. }

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

  1. -(void) viewdidload
  2. {
  3. [Super Viewdidload];
  4. NSString *str0 = @ "Rain pine Momo";
  5. NSString *str1 = @ "Rain pine Momo";
  6. String exact equality Comparison
  7. if ([Str0 ISEQUALTOSTRING:STR1])
  8. {
  9. NSLog (@ "string is exactly equal");
  10. }
  11. Strings are compared at the beginning
  12. if ([Str0 hasprefix:@ "Rain Pine"])
  13. {
  14. NSLog (@ "String str0 begins with a rain pine");
  15. }
  16. Strings are compared at the end
  17. if ([str1 hassuffix:@ "MOMO"])
  18. {
  19. NSLog (@ "str1 string ends with Momo");
  20. }
  21. }

4. string interception and case change

  1. -(void) viewdidload
  2. {
  3. [Super Viewdidload];
  4. NSString *str0 = @ "Chinese name is Xuanyusong";
  5. Intercepts the starting point of a string to the content between index 4
  6. NSString * to = [STR0 substringtoindex:4];
  7. NSLog (@ "to =%@", to);
  8. Intercepts the contents of the character index from 2 to the end
  9. NSString * from = [STR0 substringfromindex:2];
  10. NSLog (@ "from =%@", from);
  11. Set the range of intercept strings
  12. From second place to tenth place
  13. Nsrange rang = Nsmakerange (2, 10);
  14. NSString * Strrang = [Str0 Substringwithrange:rang];
  15. NSLog (@ "rang =%@", Strrang);
  16. Set the string to capitalize the first letter
  17. NSLog (@ "STR0 First Capital:%@", [Str0 capitalizedstring]);
  18. Sets the entire contents of the string to uppercase
  19. NSLog (@ "STR0 Capital:%@", [Str0 uppercasestring]);
  20. Set the character to all lowercase
  21. NSLog (@ "Str0 lowercase:%@", [Str0 lowercasestring]);
  22. }

5. searching strings and replacing strings

  1. -(void) viewdidload
  2. {
  3. [Super Viewdidload];
  4. NSString *str0 = @ "Chinese name is Xuanyusong";
  5. NSString *temp = @ "is";
  6. Nsrange rang = [Str0 rangeofstring:temp];
  7. NSLog (@ "The string searched at the starting point in Str0 of index%d", rang.location);
  8. NSLog (@ "The string searched at the end of the STR0 in the index is%d", rang.location + rang.length);
  9. Replace a string in a search with a new string
  10. NSString *str = [str0 stringbyreplacingcharactersinrange:rang withstring:@ "wow ka ka Kaka"];
  11. NSLog (@ "replacement string is%@", str);
  12. Replace All "" in the string with *
  13. str = [STR0 stringbyreplacingoccurrencesofstring: @ "" withstring:@ "@"];
  14. NSLog (@ "replacement string is%@", str);
  15. }

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

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.