IPhone objective-C string nsstring usage

Source: Internet
Author: User

During the development process, we will encounter a lot of string operations. It is a common data type, and the usage of it is summarized below:


// 1. Create a constant string.

Nsstring * astring = @ "This Is
String! ";


// 2. Create an empty string and assign a value.

Nsstring * astring = [nsstring alloc]
Init];

Astring = @ "this is a string! ";

Nslog (@ "astring: % @", astring );

[Astring release];

// 3. In the preceding method, the speed is improved by using the initwithstring method.

Nsstring * astring = [nsstring alloc]
Initwithstring: @ "this is a string! "];

Nslog (@ "astring: % @", astring );

[Astring release];

// 4. Use Standard C to create a string: initwithcstring Method

Char * cstring = "This Is
String! ";

Nsstring * astring = [nsstring alloc]
Initwithcstring: cstring];

Nslog (@ "astring: % @", astring );

[Astring release];



// 5. Create a formatted string: A placeholder (consisting of one % and one character)

Int I = 1;

Int J = 2;

Nsstring * astring = [nsstring alloc]
Initwithstring: [nsstring stringwithformat: @ "% d. This is % I
String! ", I, j];

Nslog (@ "astring: % @", astring );

[Astring release];



// 6. Create a temporary string

Nsstring * astring;

Astring = [nsstring stringwithcstring: "This
Is a temporary string "];

Nslog (@ "astring: % @", astring );


Nsstring * Path = @ "astring. Text ";

Nsstring * astring = [nsstring alloc]
Initwithcontentsoffile: path];

Nslog (@ "astring: % @", astring );

[Astring release];


Nsstring * astring = [nsstring alloc]
Initwithstring: @ "this is a string! "];

Nslog (@ "astring: % @", astring );

Nsstring * Path =
@ "Astring. Text ";

[Astring writetofile: path atomically:
Yes];

[Astring
Release];




// Compare the strcmp function with C

Char string1 [] = "string! ";

Char string2 [] = "string! ";

If (strcmp (string1, string2) =
0)

{

Nslog (@ "1 ");

}

// Isequaltostring Method

Nsstring * astring01 = @ "This Is
String! ";

Nsstring * astring02 = @ "This Is
String! ";

Bool result = [astring01
Isequaltostring: astring02];

Nslog (@ "Result: % d", result );


// Compare method (three values returned by comparer)

Nsstring * astring01 = @ "This Is
String! ";

Nsstring * astring02 = @ "This Is
String! ";

Bool result = [astring01 compare: astring02]
= Nsorderedsame;

Nslog (@ "Result: % d", result );

// Determine whether the content of nsorderedsame is the same

Nsstring * astring01 = @ "This Is
String! ";

Nsstring * astring02 = @ "This Is
String! ";

Bool result = [astring01 compare: astring02]
= Nsorderedascending;

Nslog (@ "Result: % d", result );

// Nsorderedascending
Determine the size of the two object values (in alphabetical order, astring02 is true if it is greater than astring01)

Nsstring * astring01 = @ "This Is
String! ";

Nsstring * astring02 = @ "This Is
String! ";

Bool result = [astring01 compare: astring02]
= Nsordereddescending;

Nslog (@ "Result: % d", result );

// Nsordereddescending
Determine the size of the two object values (in alphabetical order, astring02 is true if it is smaller than astring01)

// Do not consider comparing strings with uppercase and lowercase letters 1

Nsstring * astring01 = @ "This Is
String! ";

Nsstring * astring02 = @ "This Is
String! ";

Bool result = [astring01
Caseinsensitivecompare: astring02] =
Nsorderedsame;

Nslog (@ "Result: % d", result );

// Nsordereddescending determines the size of the two object values (in alphabetical order, astring02 is smaller than astring01
True)

 

// How to judge whether the string is null

Nsstring * urlstring = [urlinput stringvalue];




If
(! Urlstring ){





Nslog (
@ "No input ."
);






}
Else
{



If
([Urlstring length] =
0
){





Nslog (
@ "No input ."
);





}
Else
{





}






}


}


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.