Objective-c syntax of the nsmutablestring strings of those things

Source: Internet
Author: User

objective-c grammatical strings, those things.

nsmutablestring class inherit the NSString class, then nsstring provides the method in nsmutablestring can be used in basic, nsmutablestring Like a list of strings, it can dynamically add strings to the string Deleting a string specifies where to insert the string, and using it to manipulate the string is more flexible.

1. string Trailing add

Use Alloc after creating a string object in memory, you can dynamically manipulate the string, modify it, add it, and so on.

appendstring method: Adds a string to the tail of the string.

AppendFormat method: Add more than one type of string to the end of the string, and you can add any number and type of string.

  1. -(void) viewdidload
  2. {
  3. [Super Viewdidload];
  4. nsmutablestring * str = [[Nsmutablestring alloc] init];
  5. Add a normal string
  6. [Str appendstring:@ "AAA"];
  7. To add a string integer character type
  8. [Str appendformat:@ "My Name:%@ my age:%d my mailbox:%s", @ "Rain pine Momo", "[email protected]"];
  9. NSLog (@ "str =%@", str);
  10. }

2. deleting elements in a string

stringwithstring method: Used to create a string initialization assignment

rangeofstring method: Passing in a string returns a range within the string can also write nsmakerange (0, 3) meaning range in string 0 bits to 3 bit

Deletecharactersinrange: Delete a string a range of parameters is the extent of the deletion.

    1. -(void) viewdidload
    2. {
    3. [Super Viewdidload];
    4. Create a string
    5. nsmutablestring *str = [nsmutablestring stringwithstring:@ "rain pine momo like wow ka ka ~"];
    6. Delete characters with "Rain" in the string
    7. [STR deletecharactersinrange: [str rangeofstring: @ "Rain pine"]];
    8. NSLog (@ "str =%@", str);
    9. }

3. string Insertion

stringwithstring method: Used to create a string initialization assignment

insertstring method: The first parameter Inserts a String object, and the second parameter is inserted in the position.

    1. -(void) viewdidload
    2. {
    3. [Super Viewdidload];
    4. Create a string
    5. nsmutablestring *str = [nsmutablestring stringwithstring:@ "rain pine momo like wow ka ka ~"];
    6. Inserts a string at the 10th bit of STR
    7. [Str insertstring:@ "and Little Cutie" atindex:10];
    8. NSLog (@ "str =%@", str);
    9. }

4. String Copy

  1. -(void) viewdidload
  2. {
  3. [Super Viewdidload];
  4. Create a string
  5. nsmutablestring *str1 = [nsmutablestring stringwithstring: @ "string 1"];
  6. Nsmutablestring *str2;
  7. String Assignment
  8. STR2 = str1;
  9. [str2 appendString: @ "and string 2];
  10. NSLog (@ "str1 =%@", str1);
  11. NSLog (@ "str2 =%@", str2);
  12. }

why give str2 after adding data str1 the data has changed, too? This is the charm of the pointer, because we operate a pointer,str2 = str1 means that two pointers point to a piece of memory, then str2 point to the memory content changes after STR1 Of course changed after all.

5. string with the specified type conversion

if the parameters of the conversion are illegal, they will not throw an exception, like in Chinese to go to the integer type. No error But the conversion result is 0 , the default value.

  1. -(void) viewdidload
  2. {
  3. [Super Viewdidload];
  4. string-To-integer
  5. NSString *STR0 = @ "1121";
  6. NSString *str0 = @ "China";
  7. To turn a string strongly into an integer type
  8. int i = [Str0 intvalue];
  9. NSLog (@ "converted:%i", i);
  10. String goto Interger
  11. NSString *str1 = @ "1985";
  12. NSString *str1 = @ "China";
  13. Turn the string into a Interger
  14. Nsinteger II = [str1 integervalue];
  15. NSLog (@ "converted:%i", ii);
  16. String to Double
  17. NSString *str2 = @ "3.145926";
  18. NSString *str2 = @ "China";
  19. To turn a string into a double
  20. double d = [str2 doublevalue];
  21. NSLog (@ "converted:%f", D);
  22. String Turn float
  23. NSString *STR3 = @ "3.145926";
  24. NSString *STR3 = @ "China";
  25. Turn the string into a float
  26. double f = [Str3 floatvalue];
  27. NSLog (@ "converted:%f", f);
  28. }

Normal conversion results

Incoming illegal parameters Abnormal results

Objective-c syntax of the nsmutablestring strings of those things

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.