Objective-c strings with mutable strings

Source: Internet
Author: User

Knowledge points

Use of the 1.NSString class

Use of the 2.NSMutableString class

==========================================

NSString

1.NSString object vs. C type string

OC string is an object C string that is just a single letter stitching

OC string definition requires @ C string not required

When printing oc string%@ C string print%s

Conversion of 2.c language strings and NSString

Stringwithcstring:encoding: Functions

Function: Converts a C string to an OC string object by the specified encoding

How 3.NSString is created

1) based on the existing C string creation

2) stringWithFormat function stitching string < important >

Function: Created according to the specified format

3) initwithutf8string function

Function: By manual memory allocation and initialization of the NSString object with a C string

4) stringwithstring function

Function: To an identical string object through an existing NSString object

==================================

Common methods of NSString

1. Compare two strings for equality

Think: Whether you can compare two strings equal by S1 = = S2

Isequaltostring Functions < Important >

Function: Compares 2 strings for equality, and returns Yes if equal, no

2. How to get the length of a string

1) Length Function < important >

Function: Gets the length of the specified string object, returning Nsuinteger

3. String conversion to Digital

-(double) doublevalue;

-(float) floatvalue;

-(int) intvalue;

-(Nsinteger) IntegerValue;

-(long long) longlongvalue;

-(BOOL) boolvalue;

4. Uppercase and lowercase conversions

-(NSString *) uppercasestring; Turn into uppercase

-(NSString *) lowercasestring; Turn into lowercase

5. Judging the prefix

Suffix: hassuffix

prefix: hasprefix:

6 string concatenation

1) stringbyappendingstring Simple string concatenation

2) stringbyappendingpathcomponent Stitching path string

7. How to compare the size of two strings < important >

Compare function S1 = = S2

Function: Compares the size of two string objects, returning an enumeration result of a Nscomparisonresult

Nsorderedascending = 1 means that the left value is less than the right;

Nsorderedsame = 0 is equal

nsordereddescending = 1 means the right value is less than the left side;

8. How to extract substrings in a string

1) Characteratindex Function < important >

Function: Remove a character from a specified position

2) Substringfromindex function

Function: Extracts a string backwards from the specified position

3) Substringtoindex function

Function: Extracts a string from the beginning to the specified position

4) How to find another string in a string

1) rangeofstring function @ "Hello World" @ "World"

2) Search from left: rangeofstring< important >

Search from behind; rangeofstring Options:nsbackwardssearch

Role: Find target Specifies whether the string is in the target string

If present, the returned Nsrange will contain the starting position and the length

If not present, nsrange.location = = Nsnotfound

Exercise: 1. Find the location of the "fine" string in "Hi,i am Fine,and You"

5) substringwithrange< Important >

Function: Extracts a string from an interval position

Nsrange structure and Nsmakerange functions

Action: Creates an interval that tells the string object the range of substrings to extract

Exercise: 1. Extract "I am Fine,and You" substring from "hi,i am fine,and You"

2. Extract "Hi,i am Fine" substring from "hi,i am Fine,and You"

3. Extract the "I am fine" substring from "hi,i am Fine,and You"

--Several subscript ———

sub*****

--not counting the subscript--

range***

sub****

Practice:

Finds the number of occurrences of another string in a string;

NSString *str1 = @ "I am a string string string"

NSString *str2 = @ "string"

=================================

Nsmutablestring

The relationship between 1.NSMutableString and NSString

2. How to create a Nsmutablestring object

1) String function

Function: Creates an empty string of variable strings object

=================================

Common methods of Nsmutablestring

1. How to reset the contents of nsmutablestring

1) setstring function

Function: Sets the contents of the current string to a new string

2. How to add content after a string

1) appendstring function

Function: Appends a new string to the end of the current string

2) AppendFormat Function < important >

Function: Append by specifying format

Exercise: 1. Define name, age, gender variables and initialize them separately.

Finally, the variables are appended to the "Hello" string by the above method.

3. How to insert content in strings < important >

1) Insertstring:atindex function

Function: Inserts the specified string into the target string at the specified location

4. How to delete the contents of a string

1) Deletecharactersinrange function

Function: Deletes a string in the specified range of Nsrange

5. How to modify one piece of content < important >

1) replacecharactersinrange:withstring function

Function: Replaces a string in the specified range with the specified content Nsrange

Practice:

Reverse of string:

NSString *s = @ "Fire in the Hole";

@ "Eloh eht ni Erif";

Length, char*****, append****

Idea: Starting from the last character of the string, a word gets it out, added to the variable string;

String words in reverse order:

@ "Fire in the Hole";

Each word is separated by a single space:

@ "Hole the Fire";

RangeOf:options:NSBackWarkSearch, Substringfromindex:, AppendString, Substringtoindex:

while (1)

{

}

Idea: Starting from the back to find the space "", get the following string, add to a variable string after, get the substring continue to find;

==================================

The description method in a class

The description method is an instance method of the NSObject class, and all objective-c classes are subclasses of the NSObject class, so all Objective-c objects have a description method. This method is commonly used to implement self-describing information printing.

The default implementation of the description method is to return the format: < class name: Address >;

Rewrite:

-(NSString *) description

{

A string of return OC;

}

=========================================================

Homework:

1. Delete the to in the string Welcome to Qianfeng

2. Hello World insert Qianfeng:hello Qianfeng World

3, @ "When I am young, I loved a girl on neighbour class." Replace part:

When I am teacher, I rather teaching the students on neighbour class.

4. In string when I is young, I loved a girl in neighbor class. Find string was Young,i loved a girl

5, put three strings my name is Sean,i come from china,i am the years old link up into a string;

6, Welcome to Guangzhou according to the range to find the string come

7, Welcome to Guangzhou Find the string Guangzhou string range;

Objective-c strings with mutable strings

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.