Basic usage of nsstring and nsmutablensstring

Source: Internet
Author: User

////main.m//NSString/** NSString 1.NSString is a non-changeable string Object 2.NSMutableString is a mutable string.  The following code is a string: Add, delete, replace, check method to demonstrate. */#import<Foundation/Foundation.h>voidmutablensstring () {nsmutablestring*mutablestring =[nsmutablestring stringWithFormat:@"I am a mutable string"]; //Add a string to the specified position[Mutablestring insertstring:@"Increase"atindex: ([mutablestring length])]; NSLog (@"%@", mutablestring); //specify a range to replace all strings[Mutablestring Replacecharactersinrange:nsmakerange (0, [mutablestring length]) withstring:@"string after substitution"]; NSLog (@"%@", mutablestring); Nsrange rang= [Mutablestring rangeofstring:@"string"]; [Mutablestring Replacecharactersinrange:rang withstring:@"string"]; NSLog (@"%@", mutablestring); //Reset Variable String contents[Mutablestring setString:@"Abcdaa"]; //Replace A with E, in the form of a size difference[Mutablestring replaceoccurrencesofstring:@"A"Withstring:@"E"Options:nsliteralsearch Range:nsmakerange (0, [mutablestring length])]; NSLog (@"%@", mutablestring); //Delete A string of the specified rangeNsrange Deleterang = [mutablestring rangeofstring:@"a"]; //It is best to decide whether to exist before deleting, or to catch an exception using @try[mutablestring Deletecharactersinrange:deleterang]; NSLog (@"%@", mutablestring);}voidBasestringmeth () {//basic methods of stringsNSString *STR1 =@"Hello NSString"; //length of query stringNsuinteger length=[str1 length]; NSLog (@"length =%lu", length); //Convert UppercaseNSString *uppernsstring =[str1 uppercasestring]; NSLog (@"%@", uppernsstring); //Convert lowercaseNSString *lowernsstring =[str1 lowercasestring]; NSLog (@"%@", lowernsstring); //Character Exchange linksNSString *appendstring =@"study"; NSString*afterappendstring =[appendString STRINGBYAPPENDINGSTRING:STR1]; NSLog (@"%@", afterappendstring); //determine if strings are equalNSString *a =@"string A"; NSString*b =@"String B"; NSString*c =@"string A"; NSString*d =@"String a"; NSString*e =@"Strina a"; if([a isequaltostring:b]! =YES) {NSLog (@"Not Equal"); }    if([a isequaltostring:c]) {NSLog (@"Equal"); }    //This judgment is strictly case-sensitive .    if([a isequaltostring:d]) {NSLog (@"Equal"); }    //string comparison Size, string comparison is actually comparedNSString *compare1=@"1"; NSString*compare2 =@"2"; //-(Nscomparisonresult) Compare: (NSString *) string; The comparison method return value is Nscomparisonresult, and he is an enumeration type//typedef ns_enum (Nsinteger, nscomparisonresult) {nsorderedascending = -1l, Nsorderedsame, nsordereddescending};    if([compare1 compare:compare2] = =nsorderedascending) {NSLog (@"Compare1 <compare2"); }Else if([compare1 compare:compare2] = =nsorderedsame) {NSLog (@"Compare1 ==compare2"); }Else if([compare1 compare:compare2] = =nsordereddescending) {NSLog (@"Compare1 >compare2"); } nsstring*compare3=@"res1"; NSString*compare4 =@"RES1"; if([compare3 compare:compare4] = =nsorderedascending) {NSLog (@"compare3 <compare4"); }Else if([compare3 compare:compare4] = =nsorderedsame) {NSLog (@"compare3 ==compare4"); }Else if([compare3 compare:compare4] = =nsordereddescending) {NSLog (@"compare3 >compare4"); }    //comparison of ignoring uppercase and lowercase    if([compare3 caseinsensitivecompare:compare4] = =nsorderedascending) {NSLog (@"compare3 <compare4"); }Else if([compare3 caseinsensitivecompare:compare4] = =nsorderedsame) {NSLog (@"compare3 ==compare4"); }Else if([compare3 caseinsensitivecompare:compare4] = =nsordereddescending) {NSLog (@"compare3 >compare4"); }        }voidNsstringsearch () {//Searching for stringsNSString *STR1 =@"Hello NSString"; NSString*hello =@"Hello"; //find the location of the Hello stringNsrange Hellorang =[str1 Rangeofstring:hello]; NSLog (@"Hello Location=%lu and Length=%lu", hellorang.location,hellorang.length); NSString*hello2 =@"Hello2"; Nsrange helloRang2=[str1 Rangeofstring:hello2]; //The system returns a nsnotfound when the corresponding string is not found    if(Hellorang2.location = =nsnotfound) {NSLog (@"in%@, the corresponding string is not found%@", Str1,hello2); }    /** Incoming Nsstringcompareoptions enumeration value enum{Nscaseinsensitivesearch = 1,//case insensitive comparison nsliteralsearch = 2,//differentiated Case comparison Nsbackwardssearch = 4,//start Search from the end of the string nsanchoredsearch = 8,//Search restricted string nsnumbericsearch = 64//is based on the number in the string and calculate the order. For example Foo2.txt < Foo7.txt < Foo25.txt//The following definition is higher than Mac OS 10.5 or more than iphone 2.0 available Nsdiacriticinsensitivesearch = 12  8,//ignores the "-" symbol comparison Nswidthinsensitivesearch = 256,//ignores the length of the string, compares the result Nsforcedorderingsearch = 512//ignores the case-insensitive comparison option, and forces the return Nsorderedascending or nsordereddescending//The following definition is higher than the iphone 3.2 available Nsregularexpressionsearch = 1024//can only be applied to Rangeofs Tring: ..., stringbyreplacingoccurrencesofstring: ... and replaceoccurrencesofstring: ... Method. Using a generic compatible comparison method, if you set this, you can remove Nscaseinsensitivesearch and Nsanchoredsearch}*/    //specifies the range to find. We can use the options to determine the way the query is done. Start from the beginning or at the endNsrange nsstringrang= [str1 rangeofstring:@"NSString"Options:nscaseinsensitivesearch Range:nsmakerange (0, [str1 length])]; NSLog (@"Location=%lu,length=%lu", nsstringrang.location,nsstringrang.length); }//Interception of StringsvoidSubString () {//string InterceptionNSString *STR1 =@"Hello NSString"; //start at the end of the 5th character interceptNSString *substring = [str1 substringfromindex:5]; NSLog (@"%@", subString);//output NSStringNsrange rang = {0,5}; //specify a range to interceptNSString *subbyrang =[str1 Substringwithrange:rang]; NSLog (@"%@", Subbyrang);//Output Hello//intercept to the specified position starting from 0NSString *subtoposition= [str1 substringtoindex:5]; NSLog (@"%@", subtoposition);//Output Hello}//Creation of Stringsvoidcreatensstring () {//Creation of StringsNSString *STR1 =@"Hello NSString"; //static method to create a string,NSString *STR2 = [NSString stringWithFormat:@"nsstring%i",2]; //dynamic creation of a string, due to the dynamic creation of the counter will be +1 so we need to release it ourselves, STR3 initialization when there is no corresponding valueNSString *STR3 =[[NSString Alloc]init]; NSLog (@"%@", STR3);    [STR3 release]; }intMainintargcConst Char*argv[])        {@autoreleasepool {creatensstring ();        Basestringmeth ();        SubString ();        Nsstringsearch;    Mutablensstring (); }    return 0;}

Basic usage of nsstring and nsmutablensstring

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.