Related usages of the NSString class

Source: Internet
Author: User

One, nsstring string connection
nsstring* string; Result string

nsstring* string1, string2; A string that already exists

1. String = [NSString initwithformat:@ "%@,%@", string1, string2];
2. String = [string1 stringbyappendingstring:string2];</p>
3. string = [string stringbyappendingformat:@ "%@,%@", string1, string2];

4. string = [string stringbyappendingformat:@ "%@%@%@%@%@%@", string1, string2, String3, String4 ...];

Can be spliced a lot,%@ in the middle of a comma string also with a comma

Ii. usage of nnstring

----- methods for creating strings-----//1, create constant string NSString *astring = @ "This is a string!"; 2, first create an empty string, and then assign the value;//Alloc and init combination is suitable for passing parameters between functions, after the use of manual release NSString *astring = [[NSString alloc] init];
Astring = @ "This is a string!";
NSLog (@ "astring:%@", astring);
[Astring RELEASE];//3, in the above method, lifting Speed: Initwithstring method NSString *astring = [[NSString alloc] initwithstring:@ "This is a Str Ing! "];
NSLog (@ "astring:%@", astring);
[Astring release];
4, create temporary string nsstring *astring;
astring = [NSString stringwithcstring: "This is a temporary string"];
NSLog (@ "astring:%@", astring),//OR NSString * scriptstring = [NSString stringwithstring:@ "Tell application \" Mail\ "\ r "];//5, creating a formatted string: placeholder (consisting of a% plus 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];

----- reading a string from a file -----
NSString *path = @ "Astring.text";
NSString *astring = [[NSString alloc] initwithcontentsoffile:path];
NSLog (@ "astring:%@", astring);
[Astring release];

-----write string to file----
NSString *astring = [[NSString alloc] initwithstring:@ "This is a string!"];
NSLog (@ "astring:%@", astring);
NSString *path = @ "Astring.text";
[astring Writetofile:path Atomically:yes];
[Astring release];
-----comparison of two strings-----
1. Comparison with C: strcmp function
Char string1[] = "string!";
Char string2[] = "string!";
if (strcmp (string1, string2) = = 0)
{
NSLog (@ "1");
}
2. Isequaltostring method
NSString *ASTRING01 = @ "This is a string!";
NSString *astring02 = @ "This is a string!";
BOOL result = [Astring01 isequaltostring:astring02];
NSLog (@ "result:%d", result);
3. Compare method (three values returned by comparer: nsorderedsame,nsorderedascending,nsordereddescending)
NSString *ASTRING01 = @ "This is a string!";
NSString *astring02 = @ "This is a string!";
BOOL result = [Astring01 compare:astring02] = = Nsorderedsame; Nsorderedsame judge whether the two are the same
NSLog (@ "result:%d", result);

NSString *ASTRING01 = @ "This is a string!";
NSString *astring02 = @ "This is a string!";
BOOL result = [Astring01 compare:astring02] = = nsorderedascending;
NSLog (@ "result:%d", result);
Nsorderedascending determines the size of two object values (in alphabetical order, astring02 greater than ASTRING01 is true)

NSString *ASTRING01 = @ "This is a string!";
NSString *astring02 = @ "This is a string!";
BOOL result = [Astring01 compare:astring02] = = nsordereddescending;
NSLog (@ "result:%d", result);
Nsordereddescending determines the size of two object values (in alphabetical order, ASTRING02 is less than astring01 true)
4, do not consider case comparison string 1
NSString *ASTRING01 = @ "This is a string!";
NSString *astring02 = @ "This is a string!";
BOOL result = [Astring01 caseinsensitivecompare:astring02] = = Nsorderedsame;
NSLog (@ "result:%d", result);
Nsordereddescending determines the size of two object values (in alphabetical order, ASTRING02 is less than astring01 true)
5, do not consider case comparison string 2
NSString *ASTRING01 = @ "This is a string!";
NSString *astring02 = @ "This is a string!";
BOOL result = [Astring01 compare:astring02
Options:nscaseinsensitivesearch | Nsnumericsearch] = = Nsorderedsame;
NSLog (@ "result:%d", result);
Nscaseinsensitivesearch: Case-insensitive comparison nsliteralsearch: Make a full comparison, case-sensitive nsnumericsearch: Compares the number of characters in a string, not the character value.

Third, print the log

NSLog (@ "%@", order);//Print Dictionary

NSLog (@ "%@", imageurlstring);

Related usages of the NSString class

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.