Summary of common operation methods for string NSString in Objective-c _ios

Source: Internet
Author: User
Tags lowercase


1. Create a string



(1) Constant string


 code as follows:

NSString *string = @ "I am an iosdevtip!";

(2) Common methods of Creation
 code as follows:

NSString *string = [[NSString alloc] init];


string = @ "I am an iosdevtip too!";


(3) Create a string with initwithstring 

 code as follows:

NSString *string = [[NSString alloc] initwithstring:@ "Iosdevtip is here!"];





2. Format the creation of a string



(1) INT format string


 code as follows:

int age = 20;
NSString *personage = [NSString stringwithformat:@ ' This person ' is%d ', age];

Since int formats strings, float, double, and so on, can also format strings.





(2) NSString format string


code as follows:

NSString *name = @ "Iosdevtip";
NSString *personname = [NSString stringwithformat:@ "This person name is%@", name];





3. String comparison



(1) Comparison of isequaltostring methods


 code as follows:

NSString *stingone = @ "This is a iosdevtip!";
NSString *stringtwo = @ "This is a iosdevtip!";
BOOL result = [Stingone isequaltostring:stringtwo];

(2) Comparison of compare methods
 code as follows:

BOOL result = [Stingone compare:stringtwo] = = Nsorderedsame;

Compare: Method return value type is Nscomparisonresult. And Nscomparisonresult has the following enumeration values.
 code as follows:

typedef ns_enum (Nsinteger, nscomparisonresult) {nsorderedascending = -1l, Nsorderedsame, nsordereddescending};





4. String capitalization conversion



(1) Lowercase to uppercase


 code as follows:

NSString *string = @ "This is a iosdevtip!";
[String lowercasestring];

(2) Uppercase to lowercase
 code as follows:

NSString *string = @ "This is a iosdevtip!";
[String uppercasestring];

(3) Capitalize first letter
 code as follows:

NSString *string = @ "Ligang";
NSLog (@ "string:%@", [string capitalizedstring]);

Print:




2015-07-16 23:06:11.652 iosstrongdemo[10279:3062010] String:ligang


5. Intercept string



(1) Substringtoindex intercept string


 code as follows:

NSString *string = @ "This is a operation string!";
NSString *subtostring = [string Substringtoindex:6];

(2) The intercepted subtostring is this I





Substringfromindex Intercept string


 code as follows:

NSString *subfromstring = [string Substringfromindex:6];

(3) The intercepted subfromstring is S-a operation string!





Substringwithrange Intercept string


 code as follows:

NSString *rangestring = [String Substringwithrange:nsmakerange (6, 3)];

The intercepted rangestring is S-A!





6. Determine if the string contains another string



(1) Rangeofstring judgment


 code as follows:

NSString *string1 = @ "This is a iosdevtip";
NSString *string2 = @ "Iosdevtip";
Nsrange range = [string1 rangeofstring:string2];
Nsinteger location = range.location;
Nsinteger leight = range.length;
NSString *logstring = [[NSString alloc] initwithstring:[nsstring stringwithformat:@ "Location:%ld,leight:%ld", Location,leight]];
NSLog (@ "logstring:%@", logstring);

Print out:




IOSSTRONGDEMO[8837:2221170] Logstring:location:10,leight:9


(2) Determine if the prefix is included


 code as follows:

NSString *string = @ "I Love";
BOOL Ishasi = [string hasprefix:@ "I"];
BOOL Ishaschina = [string hassuffix:@ "in"];





7. Split string


 code as follows:

NSString *string = @ "This is a iosdevtip";
Nsarray *array = [string componentsseparatedbystring:@ "a"];
NSString *string1 = [array objectatindex:0];
NSString *string2 = [array objectatindex:1];
NSLog (@ "string1:%@ string2:%@", string1,string2);

Print:




2015-07-16 22:40:39.559 iosstrongdemo[10165:3055448] String1:this is  string2:iosdevtip


8. Insert String

 code as follows:


nsmutablestring *string = [[Nsmutablestring alloc] initwithstring:@ "I"];
[String insertstring:@ "Love" atindex:2];
NSLog (@ "string:%@", string);

Print:
2015-07-16 22:44:10.706 iosstrongdemo[10206:3057014] string:i Love
(1) Append string
Copy Code code as follows:

nsmutablestring *string = [[Nsmutablestring alloc] initwithstring:@ "I Love"];
[String appendstring:@ "in"];
NSLog (@ "string:%@", string);

Print:




2015-07-16 22:42:32.305 iosstrongdemo[10189:3056410] String:i Love


9. Delete string


Copy Code code as follows:

nsmutablestring *string = [[Nsmutablestring alloc] initwithstring:@ "I Love"];
[String Deletecharactersinrange:nsmakerange (2, 4)];
NSLog (@ "String1:%@", string);

Print:




2015-07-16 22:46:58.437 iosstrongdemo[10219:3057749] String1:i


10. Replacement string


code as follows:

NSString *string = @ "I Love";
NSString *replacestring = [string stringbyreplacingoccurrencesofstring:@ ' love ' withstring:@ ' like '];
NSLog (@ "replacestring:%@", replacestring);

Print:




2015-07-16 22:56:07.405 iosstrongdemo[10236:3059503] replacestring:i like


11. Remove the end of a string of spaces and line breaks


code as follows:

NSString *string = @ "I Love";
NSString *text = [string Stringbytrimmingcharactersinset:[nscharacterset whitespaceandnewlinecharacterset]];
NSLog (@ "text:%@", text);

Print:




2015-07-16 23:00:47.845 iosstrongdemo[10265:3061013] text:i Love




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.