objective-c
grammatical strings, those things.
nsmutablestring class inherit the NSString class, then nsstring provides the method in nsmutablestring can be used in basic, nsmutablestring Like a list of strings, it can dynamically add strings to the string Deleting a string specifies where to insert the string, and using it to manipulate the string is more flexible.
1. string Trailing add
Use Alloc after creating a string object in memory, you can dynamically manipulate the string, modify it, add it, and so on.
appendstring method: Adds a string to the tail of the string.
AppendFormat method: Add more than one type of string to the end of the string, and you can add any number and type of string.
- -(void) viewdidload
- {
- [Super Viewdidload];
- nsmutablestring * str = [[Nsmutablestring alloc] init];
- Add a normal string
- [Str appendstring:@ "AAA"];
- To add a string integer character type
- [Str appendformat:@ "My Name:%@ my age:%d my mailbox:%s", @ "Rain pine Momo", "[email protected]"];
- NSLog (@ "str =%@", str);
- }
2. deleting elements in a string
stringwithstring method: Used to create a string initialization assignment
rangeofstring method: Passing in a string returns a range within the string can also write nsmakerange (0, 3) meaning range in string 0 bits to 3 bit
Deletecharactersinrange: Delete a string a range of parameters is the extent of the deletion.
- -(void) viewdidload
- {
- [Super Viewdidload];
- Create a string
- nsmutablestring *str = [nsmutablestring stringwithstring:@ "rain pine momo like wow ka ka ~"];
- Delete characters with "Rain" in the string
- [STR deletecharactersinrange: [str rangeofstring: @ "Rain pine"]];
- NSLog (@ "str =%@", str);
- }
3. string Insertion
stringwithstring method: Used to create a string initialization assignment
insertstring method: The first parameter Inserts a String object, and the second parameter is inserted in the position.
- -(void) viewdidload
- {
- [Super Viewdidload];
- Create a string
- nsmutablestring *str = [nsmutablestring stringwithstring:@ "rain pine momo like wow ka ka ~"];
- Inserts a string at the 10th bit of STR
- [Str insertstring:@ "and Little Cutie" atindex:10];
- NSLog (@ "str =%@", str);
- }
4. String Copy
- -(void) viewdidload
- {
- [Super Viewdidload];
- Create a string
- nsmutablestring *str1 = [nsmutablestring stringwithstring: @ "string 1"];
- Nsmutablestring *str2;
- String Assignment
- STR2 = str1;
- [str2 appendString: @ "and string 2];
- NSLog (@ "str1 =%@", str1);
- NSLog (@ "str2 =%@", str2);
- }
why give str2 after adding data str1 the data has changed, too? This is the charm of the pointer, because we operate a pointer,str2 = str1 means that two pointers point to a piece of memory, then str2 point to the memory content changes after STR1 Of course changed after all.
5. string with the specified type conversion
if the parameters of the conversion are illegal, they will not throw an exception, like in Chinese to go to the integer type. No error But the conversion result is 0 , the default value.
- -(void) viewdidload
- {
- [Super Viewdidload];
- string-To-integer
- NSString *STR0 = @ "1121";
- NSString *str0 = @ "China";
- To turn a string strongly into an integer type
- int i = [Str0 intvalue];
- NSLog (@ "converted:%i", i);
- String goto Interger
- NSString *str1 = @ "1985";
- NSString *str1 = @ "China";
- Turn the string into a Interger
- Nsinteger II = [str1 integervalue];
- NSLog (@ "converted:%i", ii);
- String to Double
- NSString *str2 = @ "3.145926";
- NSString *str2 = @ "China";
- To turn a string into a double
- double d = [str2 doublevalue];
- NSLog (@ "converted:%f", D);
- String Turn float
- NSString *STR3 = @ "3.145926";
- NSString *STR3 = @ "China";
- Turn the string into a float
- double f = [Str3 floatvalue];
- NSLog (@ "converted:%f", f);
- }
Normal conversion results
Incoming illegal parameters Abnormal results
Objective-c syntax of the nsmutablestring strings of those things