1. Create a string
(1) Constant string
NSString *string = @ "I am an iosdevtip!";
(2) Common methods of Creation
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
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
NSString *name = @ "Iosdevtip";
NSString *personname = [NSString stringwithformat:@ "This person name is%@", name];
3. String comparison
(1) Comparison of isequaltostring methods
NSString *stingone = @ "This is a iosdevtip!";
NSString *stringtwo = @ "This is a iosdevtip!";
BOOL result = [Stingone isequaltostring:stringtwo];
(2) Comparison of compare methods
BOOL result = [Stingone compare:stringtwo] = = Nsorderedsame;
Compare: Method return value type is Nscomparisonresult. And Nscomparisonresult has the following enumeration values.
typedef ns_enum (Nsinteger, nscomparisonresult) {nsorderedascending = -1l, Nsorderedsame, nsordereddescending};
4. String capitalization conversion
(1) Lowercase to uppercase
NSString *string = @ "This is a iosdevtip!";
[String lowercasestring];
(2) Uppercase to lowercase
NSString *string = @ "This is a iosdevtip!";
[String uppercasestring];
(3) Capitalize first letter
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
NSString *string = @ "This is a operation string!";
NSString *subtostring = [string Substringtoindex:6];
(2) The intercepted subtostring is this I
Substringfromindex Intercept string
NSString *subfromstring = [string Substringfromindex:6];
(3) The intercepted subfromstring is S-a operation string!
Substringwithrange Intercept string
NSString *rangestring = [String Substringwithrange:nsmakerange (6, 3)];
The intercepted rangestring is S-A!
6. Determine if the string contains another string
(1) Rangeofstring judgment
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
NSString *string = @ "I Love";
BOOL Ishasi = [string hasprefix:@ "I"];
BOOL Ishaschina = [string hassuffix:@ "in"];
7. Split string
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
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
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