Objective-C note string operations, objective-c string

Source: Internet
Author: User

Objective-C note string operations, objective-c string

In this summary, there are some operations on strings in OC.

When a String object is created, an object whose content cannot be changed is created, which is called an immutable object. You can use the NSString class to process immutable strings. You often need to process strings and change the characters in strings. For example, you may want to delete some characters from a string or perform a search and replacement operation on the string. This type of string is processed using the NSMutableString class.

1 // 2 // main. m 3 // Number_String_List_15 4 // 5 // Created by SeanAstin on 15/12/30. 6 // Copyright©SeanAstin. all rights reserved. 7 // 8 9 # import <Foundation/Foundation. h> 10 11 int main (int argc, const char * argv []) {12 @ autoreleasepool {13 NSString * str1 = @ "This is string "; 14 NSString * str2 = @ "This is string B"; 15 NSString * res; 16 NSComparisonResult compareResult; 17 18 // calculate the value 19 NSLog (@ "Length of str1: % lu ", [str1 length]); 20 21 // copy one string to another string 22 res = [NSString stringWithStrin G: str1]; 23 NSLog (@ "copy: % @", res); 24 25 // copy a string to the end of another string 26 str2 = [str1 stringByAppendingString: str1]; 27 NSLog (@ "Concatentation: % @", str2); 28 29 // verify that the two strings are equal to 30 if ([str1 isw.tostring: res] = YES) 31 NSLog (@ "str1 = res"); 32 else33 NSLog (@ "str1! = Res "); 34 35 // verify whether a string is smaller than, equal to, or greater than the other string 36 compareResult = [str1 compare: str2]; 37 if (compareResult = NSOrderedAscending) 38 NSLog (@ "str1 <str2"); 39 else if (compareResult = NSOrderedSame) 40 NSLog (@ "str1 = str2 "); 41 else // It Must Be NSorderedDescending42 NSLog (@ "str1> str2"); 43 44 // convert string to uppercase 45 res = [str1 uppercaseString]; 46 NSLog (@ "Uppercase conversion: % s", [res UTF8String]); 47 48 // convert the string to lowercase 49 res = [str1 lowercaseString]; 50 NSLog (@ "Lowercase conversion: % @", res); 51} 52 return 0; 53}

 

 

1 // 2 // main. m 3 // Number_String_List_15 4 // 5 // Created by SeanAstin on 15/12/30. 6 // Copyright©SeanAstin. all rights reserved. 7 // 8 9 # import <Foundation/Foundation. h> 10 11 int main (int argc, const char * argv []) {12 @ autoreleasepool {13 NSString * str1 = @ "This is string "; 14 NSString * str2 = @ "This is string B"; 15 NSString * res; 16 nsange subRange; 17 18 // extract the First three characters from the string 19 res = [str1 substringToIndex: 3]; 20 NSLog (@ "First 3 chars of str1: % @", res ); 21 22 // extract the substring from index 5 to the end 23 res = [str1 substringFromIndex: 5]; 24 NSLog (@ "Chars from index 5 of str1: % @", res); 25 26 // extract the substring (6 characters) from index 8 to Index 13 27 res = [[str1 substringFromIndex: 8] substringToIndex: 6]; 28 NSLog (@ "Chars from index 8 through 13: % @", res); 29 30 // Simpler Method 31 res = [str1 substringWithRange: NSMakeRange (8, 6)]; 32 NSLog (@ "Chars from index 8 through 13: % @", res ); 33 34 // search for A string 35 subRange = [str1 rangeOfString: @ "String A"]; 36 NSLog (@ "string is at index % lu, length is % lu ", subRange. location, subRange. length); 37 subRange = [str1 rangeOfString: @ "string B"]; 38 if (subRange. location = NSNotFound) 39 NSLog (@ "String not found"); 40 else41 NSLog (@ "String is at index % lu, length is % lu", subRange. location, subRange. length); 42 43} 44 return 0; 45}

 

The NSMutableString class can be used to create string objects that can change characters. Because it is a subclass of the NSString class, all methods of the NSString class can be used.

When talking about variable strings, we talked about changing the actual characters in strings. Any variable or immutable String object can always be set as a completely different string object during program execution.

1 // 2 // main. m 3 // Number_String_List_15 4 // 5 // Created by SeanAstin on 15/12/30. 6 // Copyright©SeanAstin. all rights reserved. 7 // 8 9 # import <Foundation/Foundation. h> 10 11 int main (int argc, const char * argv []) {12 @ autoreleasepool {13 NSString * str1 = @ "This is string A"; 14 NSString * search, * replace; 15 NSMutableString * mstr; 16 nsange substr; 17 18 // create a variable string 19 20 mstr = [NSMutableString stringWithString: str1] from an immutable string; 21 NSLog (@ "% @", mstr); 22 23 // insert character 24 [mstr insertString: @ "mutable "AtIndex: 7]; 25 NSLog (@" % @ ", mstr); 26 27 // The inserted end is effectively spliced 28 [mstr insertString: @" and string B "atIndex: [mstr length]; 29 NSLog (@ "% @", mstr); 30 31 // directly use appendString32 [mstr appendString: @ "and string C"]; 33 NSLog (@ "% @", mstr); 34 35 // Delete the substring 36 by range [mstr deleteCharactersInRange: NSMakeRange (16, 13)]; 37 NSLog (@ "% @", mstr); 38 39 // search for and delete it 40 substr = [mstr rangeOfString: @ "string B and"]; 41 if (substr. location! = NSNotFound) {42 [mstr deleteCharactersInRange: substr]; 43 NSLog (@ "% @", mstr ); 44} 45 46 // directly set it to A variable string 47 [mstr setString: @ "This is string A"]; 48 NSLog (@ "% @", mstr ); 49 50 // replace some characters 51 [mstr replaceCharactersInRange: NSMakeRange (8, 8) withString: @ "a mutable string"]; 52 NSLog (@ "% @", mstr ); 53 54 // search for and replace 55 search = @ "This is"; 56 replace = @ "An example"; 57 substr = [mstr rangeOfString: search]; 58 if (substr. locat Ion! = NSNotFound) {59 [mstr replaceCharactersInRange: substr withString: replace]; 60 NSLog (@ "% @", mstr ); 61} 62 63 // search and replace all matching Items 64 search = @ "a"; 65 replace = @ "X"; 66 substr = [mstr rangeOfString: search]; 67 while (substr. location! = NSNotFound) {68 [mstr replaceCharactersInRange: substr withString: replace]; 69 substr = [mstr rangeOfString: search]; 70 71} 72 NSLog (@ "% @", mstr ); 73} 74 return 0; 75}

 

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.