Common usage of API nsstring __API

Source: Internet
Author: User

/*

Length

NSString *string = [[NSString alloc]

initwithformat:@ "SDGIJKHAJKHKHKLHDSHJGDST"];

NSLog (@ "%lu", [string length]);

-(Unichar) Characteratindex:


(Nsuinteger) index; Output a character at a specified position in a subscript

NSString *s = [[NSString

alloc]initwithformat:@ "Abckjghj"];

NSLog (@ "%c", [s characteratindex:2]);


-(NSString *) Substringfromindex:

(Nsuinteger) from; Output all strings from the ⾯ surface after the index subscript starts

NSLog (@ "%@", [string substringfromindex:4]);


-(NSString *) Substringtoindex:

(Nsuinteger) to; ⼀ one output from the start position of the string to the index subscript position

NSLog (@ "%@", [string substringtoindex:5]);


-(NSString *) Substringwithrange:

(nsrange) range; The string within the range of the output range

NSLog (@ "%@", [String Substringwithrange:nsmakerange (2, 5)]);


-(Nscomparisonresult) Compare: (nsstring*) string; ⽐ comparison between posterior ⾯ surface and anterior ⾯ physiognomy

if ([string compare:s] = = 1) {

NSLog (@ "ascending");

else if ([string compare:s] = = 1) {

NSLog (@ "descending");

} else {

NSLog (@ "equal");

}


-(Nscomparisonresult) Caseinsensitivecompare:

(NSString *) string; Case-insensitive comparisons

if ([string caseinsensitivecompare:s]) {

NSLog (@ "yes");

} else {

NSLog (@ "no");

}


-(Nscomparisonresult) Localizedcompare:

(NSString *) string; Local comparison

if ([s localizedcompare:string]) {

NSLog (@ "yes");

} else {

NSLog (@ "no");

}


// -

(Nscomparisonresult) Localizedcaseinsensitivecompare: (NSString *) string; Case-insensitive comparison of whether a previous string contains the latter string


if ([string

Localizedcaseinsensitivecontainsstring:s]) {

NSLog (@ "yes");

} else {

NSLog (@ "no");

}


-(Nscomparisonresult) Localizedstandardcompare: (NSString *) Comparison of string//Standard

if ([string localizedstandardcompare:s]) {

NSLog (@ "yes");

} else {

NSLog (@ "no");

}


-(BOOL) isequaltostring: (nsstring*) astring; To determine whether two strings are equal

if ([string isequalto:s]) {

NSLog (@ "1");

} else {

NSLog (@ "0");

}


-(BOOL) Hasprefix: (NSString *) astring; Prefix

if ([String hasprefix:@ "SD"]) {

NSLog (@ "yes");

} else {

NSLog (@ "no");

}


-(BOOL) Hassuffix: (NSString *) astring; Suffix

if ([String hassuffix:@ "DST"]) {

NSLog (@ "yes");

} else {

NSLog (@ "no");

}

*/



/*

-(BOOL) containsstring: (NSString *) astring//whether contains strings

if ([String containsstring:@ "a"]) {

NSLog (@ "yes");

} else {

NSLog (@ "no");

}

-(BOOL) Localizedcaseinsensitivecontainsstring:

(NSString *) astring//case-insensitive whether or not to contain strings

if ([String containsstring:@ "D"]) {

NSLog (@ "yes");

} else {

NSLog (@ "no");

}


-(Nsrange) rangeofstring: (nsstring

*) astring; The coordinates of the output string

Nsrange r1 = [string rangeofstring:@ "a"];

NSLog (@ "{%lu,%lu}", R1.location, R1.length);

-(Nsrange) Rangeofcharacterfromset:

(Nscharacterset *) aset;


Nsrange R3 = [string

Rangeofcomposedcharactersequenceatindex:3];

NSLog (@ "{%lu,%lu}", R3.location, R3.length);


-(NSString *) stringbyappendingstring: (nsstring*) astring; Stitching strings

-(NSString *) Stringbyappendingformat: (nsstring*) format, ...//format stitching string

NSLog (@ "%@", string);

*/



-(Nsarray *) componentsseparatedbystring: (NSString *) separator;

Nsarray *array= [[Nsarray alloc]

initwithobjects:@ "123", @ "SDF", @ "456", @ "DFG", nil];

[Array componentsjoinedbystring:@ "S"];

NSLog (@ "%@", array);

//

NSLog (@ "%@", [string uppercasestring]); Uppercase output

NSLog (@ "%@", [string lowercasestring]); lowercase output

NSLog (@ "%@", [string capitalizedstring]); Capitalize first letter


//

NSString * str = [[NSString alloc]

initwithformat:@ "Qwerrtyyu"];

NSLog (@ "%@", [str description]);

// */



/*

@property (readonly) Nsuinteger hash; Converts letters to the corresponding hash value

NSLog (@ "%lu", [@ "F" hash]);

*/



Search criteria

/*

enum{

Nscaseinsensitivesearch = 1,//case-insensitive comparison

Nsliteralsearch = 2,//Case Sensitive comparison

Nsbackwardssearch = 4,//Starting at end of string

Nsanchoredsearch = 8,//A string of search limit ranges

Nsnumbericsearch = 64//The order according to the numbers in the string. For example Foo2.txt < Foo7.txt < Foo25.txt

The following definitions are higher than the Mac OS 10.5 or ⾼ above the iphone 2.0 available

Nsdiacriticinsensitivesearch = 128,//ignores the comparison of "-" symbols

Nswidthinsensitivesearch = 256,//ignores the ⻓ length of the string, comparing the results

Nsforcedorderingsearch = 512//ignores options for case-insensitive comparisons and forces return nsorderedascending or nsordereddescending

The following definitions ⾼ above the iphone 3.2 can be ⽤

Nsregularexpressionsearch = 1024//can only be applied to

Rangeofstring:..,

Stringbyreplacingoccurrencesofstring: ... and replaceoccurrencesofstring: ... Method. Using a universally compatible comparison method, if you set this item, you can remove Nscaseinsensitivesearch and

Nsanchoredsearch

}*/




/*

Variable string

nsmutablestring *mstring = [[Nsmutablestring alloc] initwithformat:@ "Master and apprentice four people and Demons"];

[Mstring insertstring:@ "Western Heaven to Learn" atindex:4];

NSLog (@ "%@", mstring);

[Mstring Deletecharactersinrange:nsmakerange (4,4)];

NSLog (@ "%@", mstring);

[Mstring appendformat:@ "Between heaven and Earth"];

NSLog (@ "%@", mstring);

[Mstring appendstring:@ "No one can Tube"];

NSLog (@ "%@", mstring);

-(Nsmutablestring *) Initwithcapacity:

(Nsuinteger) capacity;


Initialization with capacity

+ (nsmutablestring *) Stringwithcapacity:

(Nsuinteger) capacity;


Convenience Builder with capacity

[Mstring replaceoccurrencesofstring:@ "Between Heaven and Earth"

withstring:@ "from east to west to the Holy Land"

Options:nsbackwardssearch Range:nsmakerange (8, 8)];

NSLog (@ "%@", mstring);


Estimate how much space you want to apply, equivalent to a unit

Nsmutablestring *string2 =

[[Nsmutablestring alloc] initwithcapacity:10];

[string2 appendstring:@ "haha"];

NSLog (@ "%@", string2);

//

nsmutablestring * String3 =

[[Nsmutablestring Alloc]

initwithformat:@ "XXXXXXX.PCG"];

if ([String3 hassuffix:@ "PNG"]) {

Nsrange range = [string3

rangeofstring:@ "PNG"];

[String3 Replacecharactersinrange:range

withstring:@ "JPG"];

} else {

[String3 appendstring:@ "jpg"];

// }

NSLog (@ "%@", string3);

*/

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.