Immutable string
Create a String object
nsstring *string = [[nsstring alloc] initwithformat:@ "Zhong Huang-"];
nsstring *string1 = [[nsstring alloc] initwithformat:@ "Zhonger ' s is% D ", ";
nsstring *string2 = [[nsstring alloc] initwithformat:@ "Zhonger ' s gender is%@" ,@ " ominous "];
nsstring *string3 = @ "I love IOS";
gets the length of the string
unsigned long length = [string2 length];
get the characters in a string
unichar character = [string2 characteratindex:];
// Compare the contents of two strings
BOOL isequal = [string3 isequaltostring: String4];
string comparison
nscomparisonresult result = [String3 compare: String4];
To intercept a string:
//1.substringfromindex:
// start from one subscript to the last
nsstring *newstring = [string substringfromindex:5];
//2.substringtoindex:
// intercept from the beginning and end with a mark
nsstring *newstring1 = [string substringtoindex:5];
//3.substringwithrange:
// intercept a few characters from the beginning and intercept 3 from the second one .
//Nsmakerange Select several locations from a subscript
//nsmakerange (< #NSUInteger Loc#>, < #NSUInteger len#>)
nsstring *newstring2 = [string substringwithrange:nsmakerange(2, 3 )];
// splicing string stringbyappendingformat
nsstring *newstr = [string stringbyappendingformat: The force of @ ]; You can also print placeholders
nsstring *newstr1 = [string stringbyappendingformat:@ " Force %d", 2];
nsstring *newstr2 = [string stringbyappendingstring:@ " Force 2"];
nsstring *newstr3 = [string stringbyappendingstring: string1]; // can not print placeholder ...
// replace String.
nsstring *news = [string2 stringbyreplacingcharactersinrange:nsmakerange( 2 ) withstring:@ " hermaphrodite "];
when you do not know the exact position of the replaced interior, and, if there are multiple, replace all
nsstring*news1 = [string2 stringbyreplacingoccurrencesofstring:@ " ominous " Withstring:@ " hermaphrodite "];
// string converted to int type
nsstring *numberstr = @ "123";
int number = [numberstr intvalue];
// string uppercase , lowercase , first letter uppercase
nsstring *resultstr = [string uppercasestring];
nsstring *resultstr1 = [string lowercasestring];
nsstring *resultstr2 = [string capitalizedstring];
// Determines whether the string begins in the specified manner or ends in any specified manner.
BOOL isTrue = [string hasprefix:@ "zh"];
BOOL isTrue1 = [string hassuffix:@ "z"];
Variable string
creates a variable string, with a capacity of 0, and thesystem automatically applies the space based on the contents of the variable string.
nsmutablestring *mstr = [[nsmutablestring alloc] initwithcapacity:0 ];
// set content
[Mstr setString:@ "The force of the primitive, has not been able to stop, Kylin force "];
// splicing
[Mstr AppendFormat:@ ", too good to be snapped !" ];
// Delete characters in a range
[Mstr deletecharactersinrange:nsmakerange(6, 2)];
// inserts a specified string starting from a subscript
[Mstr insertstring:@ " two " atindex:2];
// Replace a string in a range
[Mstr replacecharactersinrange:nsmakerange(8, 2) withstring:@ " Devour "];
NSLog(@ "MSTR is%@", MSTR);
Note that the string is aware of the use of methods, so be sure to remember the method name, if you do not know, you can flip through the API documentation. There is a detailed description of all the methods in the API documentation.
Immutable string nsstring with variable string nsmutablestring