OC string NSString, ocnsstring

Source: Internet
Author: User
Tags first string

OC string NSString, ocnsstring

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

Advanced object-oriented programming and strings

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

△1. Design Pattern of the Class-singleton

[Singleton] only one piece of memory space exists in a program. This type of design mode becomes Singleton.

[Q] When to use the Singleton?

[Answer] when data is shared

 

1. Writing format of the singleton

Methods Starting with + (plus sign) can be directly called by classes; Methods Starting with-(minus sign) are object methods that are called by objects;

 

2. Recognition Singleton

[See singleDemo]

 

[Note] The Name Of The Singleton method can be used at will, but it is recommended that the method be well known!

 

 

Exercise 1: Create a singleton class single with a typeid attribute. Create a dog class and a cat class;

Requirements: The dog class is responsible for modifying the attribute value of the single class, and cat is responsible for extracting the typeid value.

[Thinking] How can we achieve this without a Singleton?

 

2. get and set methods and. Attributes

 

Exercise 2: Create a cat class and implement the get and set methods in the Code. The cat class contains the speed member variable.

Requirement: the first method is get and set.

Method 2: @ property

Exercise 3: You must use @ property to modify the question in Exercise 1.

 

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

String

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

 

1. Recognize the strings in oc

In oc, the content contained in @ "" is a string, regardless of the length of the string contained in.

[Example]: @ "a", @ "123", @ "_ % ^ &*",

[Note] "" is used for strings in C language and @ "" is used in oc.

 

NSString

[See strDemo1]

 

// Declare a string of NSString type

// NSString is the type of A Class. Its usage is the same as that of the basic type.

// NSString is a string class encapsulated by Apple.

// Defines the str object variable and assigns the initial value @ "hello world! "

NSString * str = @ "hello world! ";

// C string

// Char a [64] = "hello ";

// A [0], a [1]...

// NSString * str1 = [[NSString alloc] initWithFormat: @ "% @", str];

// NSLog (@ "% @", str1 );

// Evaluate the string length --- length

// Common int type in oc, not common in NSUInteger

NSUInteger length = [str length];

NSLog (@ "% ld", length );

// Complete String concatenation

NSString * Str1 = @ "yudejun ";

NSString * Str2 = @ "xijinping ";

// The string format conversion character in oc is: % @

// When the compiler encounters a formatted conversion character, it will automatically replace it with the variable value corresponding to the following parameter.

NSString * Str3 = [NSString stringWithFormat: @ "% @ and % @", Str1, Str2];

NSLog (@ "% @", Str3 );

// [Exercise]

// Splice two strings. The content of the first string is @ "hello", and the second string is @ "China". Print the length after splicing.

NSString * str4 = @ "hello ";

NSString * str5 = @ "China ";

NSString * str6 = [NSString stringWithFormat: @ "% @", str4, str5];

NSString * str6 = [NSString stringWithFormat: @ "% d", 324123];

NSLog (@ "% ld", [str6 length]);

// String Conversion

NSString * StrInt = @" 123456789987654321 ";

Int a = [StrInt intValue];

NSLog (@ "% d", );

Long B = [StrInt longLongValue];

// @ "0.1234"

// NSString * StrFloat = @" 0.1234 ";

Float c = [@ "0.1234" floatValue];

// Exercise: calculate the value of @ "1234" @ "0.25" @ "199" @ "0.5" and print it out.

// String segmentation

// [Note] string segmentation must be within the Valid String Length range.

NSString * tempStr = @ "helloworld ";

// NSString * FStr = [tempStr substringFromIndex: 5];

// NSString * FStr = [tempStr substringToIndex: 5];

Nsange range = {2, 3 };

NSString * FStr = [tempStr substringWithRange: range];

 

NSLog (@ "% @", FStr );

// String comparison

NSString * tem1 = @ "abc ";

NSString * tem2 = @ "abc ";

// Determine whether tem1 is equal to tem2

If ([tem1 is1_tostring: tem2]) {

NSLog (@ "equal ");

}

Else

{

NSLog (@ "not equal ");

}

NSComparisonResult result = [tem1 compare: tem2];

If (result = NSOrderedAscending ){

NSLog (@ "tem2> tem1 ");

}

Else if (result = NSOrderedDescending)

{

NSLog (@ "tem2 <tem1 ");

} Else if (result = NSOrderedSame)

{

NSLog (@ "tm2 = tem1 ");

}

// BOOL is a boolean type. There are only two statuses: true or false. You can use 1, 0, yes, or no.

// Determine the string prefix and suffix

NSString * string = @ "http://www.baidu.com ";

BOOL have = [string hasPrefix: @ "http: //"];

If (have = YES ){

NSLog (@ "starting with http ");

}

Else

{

NSLog (@ "not starting with http ");

}

BOOL havejiewei = [string hasSuffix: @ "com"];

If (havejiewei ){

NSLog (@ "end with com ");

}

Else

{

NSLog (@ "not the end of com ");

}

// Convert to uppercase

NSLog (@ "% @", [string uppercaseString]);

// Convert to lowercase

[String lowercaseString];

// Uppercase letters of all words

[String capitalizedString];

// Search for a string and replace it. The first parameter is the string to be searched, and the second string is the string to be replaced.

String = [string stringByReplacingOccurrencesOfString: @ "www" withString: @ "googl"];

NSLog (@ "% @", string );

NSString * tmp = @ "ceshishuju ";

// ================================================ ===

NSMutableString * m_str = [[NSMutableString alloc] initWithString: tmp];

// Modify the string

[M_str setString: @ "hello world"];

// Append the string

[M_str appendFormat: @ "nihao"];

// Delete a string

Nsange rang = {1, 2 };

[M_str deleteCharactersInRange: rang];

// String insertion

[M_str insertString: @ "xijinping" atIndex: 3];

NSLog (@ "% @", m_str );

// All objects starting with NSMutable can be of variable type, and objects starting with NSMutable can be modified;

// NSString // immutable string

// NSMutableString // variable string

// NSArray // unchangeable Array

// NSMutableArray // variable array

// NSDictionary // unchangeable dictionary

// NSMutableDictionary // variable dictionary

// Variable means that the variables defined for this type can be modified or deleted.

 

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.