Objective-C syntax for nsmutablestring (4)

Source: Internet
Author: User
Tags float double
What are the strings in objective-C syntax? If the original Yusong Momo article is reprinted, please note: It is reprinted to my independent domain name blogYusong Momo program Research Institute, Original address: http://www.xuanyusong.com/archives/388

  The nsmutablestring class inherits the nsstring class, so the methods provided by nsstring can be basically used in nsmutablestring. nsmutablestring is like a string linked list, it can dynamically Add a string to delete a string, insert a string at a specified position, and use it to operate the string more flexibly.

1. Add string tail
Using alloc to create a String object in memory, you can dynamically operate on this string, modify and add it. Appendstring method: adds a string to the end of the string. Appendformat: adds multiple types of strings to the end of a string. You can add any number or type of strings.

-(Void) viewdidload {[Super viewdidload]; nsmutablestring * STR = [[nsmutablestring alloc] init]; // Add a common string [STR appendstring: @ "AAA"]; // Add the string Integer type [STR appendformat: @ "my name: % @ My age: % d my email: % s", @ "Yu Song Momo", 25, "xuanyusong@gmail.com"]; nslog (@ "str = % @", STR );}




2. delete an element from a string
Stringwithstring method: used to create a string to initialize the value assignment.Rangeofstring method: the input string returns a range in the string, which can also be written.Nsmakerange (0, 3) indicates that the range is 0 to 3rd characters in the string.Deletecharactersinrange: The range parameter for deleting a string is the deleted range.

-(Void) viewdidload {[Super viewdidload]; // create a string nsmutablestring * STR = [nsmutablestring stringwithstring: @ "Yu Song Momo prefers wow ~ "]; // Delete [STR deletecharactersinrange: [STR rangeofstring: @" Yu Song "]; nslog (@" str = % @", str );}



3. String insertion
Stringwithstring method: used to create a string to initialize the value assignment.
Insertstring method: The String object inserted by the first parameter and the position inserted by the second parameter.

-(Void) viewdidload {[Super viewdidload]; // create a string nsmutablestring * STR = [nsmutablestring stringwithstring: @ "Yu Song Momo prefers wow ~ "]; // Insert the string [STR insertstring: @" "atindex: 10]; nslog (@" str = % @ ", STR) in STR 10th );}



4. Copy strings
-(Void) viewdidload {[Super viewdidload]; // create a string nsmutablestring * str1 = [nsmutablestring stringwithstring: @ "string 1"]; nsmutablestring * str2; // string value str2 = str1; [str2 appendstring: @ "and string 2"]; nslog (@ "str1 = % @", str1 ); nslog (@ "str2 = % @", str2 );}

Why does the str1 data change after str2 is added? This is the charm of the pointer, because we operate on the pointer, str2 = str1 means that the two pointers point to a piece of memory at the same time, then the memory content pointed to by str2 will change after str1.





5. String and specified type conversion
If the converted parameter is invalid, no exception will be thrown, for example, converting the integer type in Chinese. No error is reported, but the conversion result is 0. The default value is.

-(Void) viewdidload {[Super viewdidload]; // convert a string to an integer nsstring * str0 = @ "1121"; // nsstring * str0 = @ "China "; // convert the string to an integer int I = [str0 intvalue]; nslog (@ "converted: % I", I ); // convert the string to interger nsstring * str1 = @ "1985"; // nsstring * str1 = @ "China"; // convert the string to interger nsinteger II = [str1 integervalue]; nslog (@ "converted: % I", ii); // convert the string to double nsstring * str2 = @ "3.145926"; // nsstring * str2 = @ "China "; // convert the string to double D = [str2 doublevalue]; nslog (@ "after conversion: % F", d ); // convert the string to float nsstring * str3 = @ "3.145926"; // nsstring * str3 = @ "China"; // convert the string to float double F = [str3 floatvalue]; nslog (@ "converted: % F", f );}

Normal Conversion Result



Invalid input parameter results



6. For more information about string search and replacement, see the previous chapter.
Nsstring in objective-C syntax (3)


Welcome to everyone, big, small, old, and few male women, to talk about iPhone development ~~ I am waiting here ~ Wow

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.