In Objective-C, NSString and NSMutableString are core classes that process strings. The biggest difference between these two classes is that after NSString is created and assigned a value, the content and length of the string cannot be dynamically changed, unless this string is assigned again. The content and length of the NSMutableString can be changed dynamically after being assigned a value.
1. Create an NSString
The biggest difference between NSString and char * is that NSString is an objective object, while char * is a byte array. @ + "String" indicates the standard usage of the objective-c NSString String constant. You do not need to add @ when creating a char @
C code-(void) viewDidLoad
{
[Super viewDidLoad];
// Classic string value assignment
NSString * str0 = @ "my name is justcoding! ";
// String formatting and merging include
// NSString * type int type char * type
NSString * str1 = [NSString stringWithFormat: @ "my name: % @ My age: % d my email: % s", @ "justcoding", 25, justcoding@gmail.com];
// Only one string can be written in the string assignment parameter, which is similar to the first one.
NSString * str2 = [NSString stringWithString: @ "I Am a string"];
// Convert the string to the UTF-8 format. The parameter is of the char * type.
NSString * str3 = [NSString stringwithuf8string: "string to UTF-8 format"];
// String Merging
Int I = 100;
Char * c = "xuanyusong ";
NSString * temp = @ "I Am a temporary string ";
// Add int I and char * c to form a new string based on the string temp
NSString * str4 = [temp stringByAppendingFormat: @ "INTEGER: % d character type: % s", I, c];
// Add temp Based on the string temp and form a new string
NSString * str5 = [temp stringByAppendingString: temp];
// String output
NSLog (@ "str0 = % @", str0 );
NSLog (@ "str1 = % @", str1 );
NSLog (@ "str2 = % @", str2 );
NSLog (@ "str3 = % @", str3 );
NSLog (@ "str4 = % @", str4 );
NSLog (@ "str5 = % @", str5 );
}
2. String Traversal
Each string is actually composed of several char characters. The traversal of a string is actually to extract each character from the string.
C code-(void) viewDidLoad
{
[Super viewDidLoad];
// Classic string value assignment
NSString * str = @ "YUSONGMOMO ";
// String Length
Int count = [str length];
NSLog (@ "the string length is % d", count );
// Traverse every character in the string
For (int I = 0; I <count; I ++)
{
Char c = [str characterAtIndex: I];
NSLog (@ "the character string % d is % c", I, c );
}
}
3. String comparison
Whether the isEqualToString compares strings are completely equal, and cannot be completely matched if the case is different.
HasPrefixe matches string Headers
HaSuffix matches the string's tail
C code-(void) viewDidLoad
{
[Super viewDidLoad];
NSString * str0 = @ "justcoding ";
NSString * str1 = @ "justcoding ";
// Exact comparison of strings
If ([str0 is1_tostring: str1])
{
NSLog (@ "the string is completely equal ");
}
// String start
If ([str0 hasPrefix: @ "just"])
{
NSLog (@ "string str0 starts with just ");
}
// String end-to-end comparison
If ([str1 hasSuffix: @ "coding"])
{
NSLog (@ "str1 string ending with coding ");
}
}
C code // isdomaintostring Method
NSString * astring01 = @ "This is a String! ";
NSString * astring02 = @ "This is a String! ";
BOOL result = [astring01 isw.tostring: astring02];
NSLog (@ "result: % d", result );
C code // compare method (three values returned by comparer)
NSString * astring01 = @ "This is a String! ";
NSString * astring02 = @ "This is a String! ";
BOOL result = [astring01 compare: astring02] = NSOrderedSame;
NSLog (@ "result: % d", result );
// Determine whether the content of NSOrderedSame is the same
C code NSString * astring01 = @ "This is a String! ";
NSString * astring02 = @ "this is a String! ";
BOOL result = [astring01 compare: astring02] = NSOrderedAscending;
NSLog (@ "result: % d", result );
// NSOrderedAscending determines the size of the two objects (which is compared alphabetically. astring02 is true if it is greater than astring01)
C code NSString * astring01 = @ "this is a String! ";
NSString * astring02 = @ "This is a String! ";
BOOL result = [astring01 compare: astring02] = NSOrderedDescending;
NSLog (@ "result: % d", result );
// NSOrderedDescending determines the size of the two object values (which are compared alphabetically. astring02 is true if it is smaller than astring01)
C code // do not consider comparing strings with uppercase and lowercase letters 1
NSString * astring01 = @ "this is a String! ";
NSString * astring02 = @ "This is a String! ";
BOOL result = [astring01 caseInsensitiveCompare: astring02] = NSOrderedSame;
NSLog (@ "result: % d", result );
// NSOrderedDescending is used to determine the size of the two object values (which are compared alphabetically. astring02 is true if it is smaller than astring01)
C code // compare string 2 with case insensitive
NSString * astring01 = @ "this is a String! ";
NSString * astring02 = @ "This is a String! ";
BOOL result = [astring01 compare: astring02 options: NSCaseInsensitiveSearch | NSNumericSearch] = NSOrderedSame;
NSLog (@ "result: % d", result );
// NSCaseInsensitiveSearch: case-insensitive comparison NSLiteralSearch: performs a full comparison. case-sensitive NSNumericSearch: compares the number of characters of a string, not the character value.
The method of determination can use caseInsensitiveCompare in the NSString category to determine the order relationship between two strings by returning the values of-1, 0, or 1. The program is as follows.
C code NSString * string = @ "0 ";
NSComparisonResult result = [string caseInsensitiveCompare: @ "A"];
Switch (result ){
Case NSOrderedAscending:
NSLog (@ "power-up ");
Break;
Case NSOrderedSame:
NSLog (@ "ignore strings of the same case ");
Break;
Case NSOrderedDescending:
NSLog (@ "power down ");
Break;
Default:
NSLog (@ "unable to determine ");
Break;
}
4. File string operations (read/write)
C code // read the string from the file: initWithContentsOfFile Method
NSString * path = @ "astring. text ";
NSString * astring = [[NSString alloc] initWithContentsOfFile: path];
NSLog (@ "astring: % @", astring );
[Astring release]; // astring = nil;
C code // write a string to a file: writeToFile Method
NSString * astring = [[NSString alloc] initWithString: @ "This is a String! "];
NSLog (@ "astring: % @", astring );
NSString * path = @ "astring. text ";
[Astring writeToFile: path atomically: YES];
[Astring release]; // astring = nil;
* Ios5 does not support release iOS Application Development: What is ARC?
C code // extension path
NSString * Path = @"~ /NSData.txt ";
NSString * absolutePath = [Path stringByExpandingTildeInPath];
NSLog (@ "absolutePath: % @", absolutePath );
NSLog (@ "Path: % @", [absolutePath stringByAbbreviatingWithTildeInPath]);
C code // File Extension
NSString * Path = @"~ /NSData.txt ";
NSLog (@ "Extension: % @", [Path pathExtension]);
5. String truncation and Case sensitivity
C code // change the case sensitivity of the string
NSString * string1 = @ "A String ";
NSString * string2 = @ "String ";
NSLog (@ "string1: % @", [string1 uppercaseString]); // uppercase
NSLog (@ "string2: % @", [string2 lowercaseString]); // lower case
NSLog (@ "string2: % @", [string2 capitalizedString]); // initial size
Java code-(void) viewDidLoad
{
[Super viewDidLoad];
NSString * str0 = @ "Chinese my name is xuanyusong ";
// Intercept the content from the string start point to index 4
NSString * to = [str0 substringToIndex: 4];
NSLog (@ "to = % @", );
// Intercept the content between 2 and the end of the Character index
NSString * from = [str0 substringFromIndex: 2];
NSLog (@ "from = % @", from );
// Set the range of the truncated string
// Starts from the second digit and the length is 10
Nsange rang = NSMakeRange (2, 10 );
NSString * strRang = [str0 substringWithRange: rang];
NSLog (@ "rang = % @", strRang );
// Set uppercase letters of the string
NSLog (@ "str0 uppercase: % @", [str0 capitalizedString]);
// Set all strings to uppercase
NSLog (@ "str0 capital: % @", [str0 uppercaseString]);
// Set all characters to lowercase
NSLog (@ "str0 lower case: % @", [str0 lowercaseString]);
}
6. Search for and replace strings
C code-(void) viewDidLoad
{
[Super viewDidLoad];
NSString * str0 = @ "Chinese my name is xuanyusong ";
NSString * temp = @ "is ";
Nsange rang = [str0 rangeOfString: temp];
NSLog (@ "the index of the start point of the searched string in str0 is % d", rang. location );
NSLog (@ "the index of the string to be searched in str0 is % d", rang. location + rang. length );
// Replace the string in the search with a new string
NSString * str = [str0 stringByReplacingCharactersInRange: rang withString: @ "Kaka"];
NSLog (@ "replaced string: % @", str );
// Replace "" in the string *
Str = [str0 stringByReplacingOccurrencesOfString: @ "" withString: @ "@"];
NSLog (@ "replaced string: % @", str );
}
Extended: You can use the following method to replace the entire string and set the region to be replaced.
StringByReplacingOccurrencesOfString :( NSString *) withString :( NSString *) options :( NSStringCompareOptions) range :( nsange)
7. Add the string tail
Using alloc to create a String object in memory, you can dynamically operate on this string, modify and add it.
Appendstring method: adds a string to the end of the string.
AppendFormat: adds multiple types of strings to the end of a string. You can add any number or type of strings.
C code-(void) viewDidLoad
{
[Super viewDidLoad];
NSMutableString * str = [NSMutableString alloc] init];
// Add a normal string
[Str appendString: @ "aaa"];
// Add the string Integer type
[Str appendFormat: @ "my name: % @ My age: % d my mailbox: % s", @ "justcoding", 25, "justcoding@gmail.com"];
NSLog (@ "str = % @", str );
}
8. delete an element from a string
StringWithString method: used to create a string to initialize the value assignment.
RangeOfString method: input a string to return a range in the string. You can also write NSMakeRange (0, 3) to indicate that the range is 0 to 3rd characters in the string.
DeleteCharactersInRange: The Range parameter for deleting a string is the deleted Range.
C code-(void) viewDidLoad
{
[Super viewDidLoad];
// Create a string
NSMutableString * str = [NSMutableString stringWithString: @ "justcoding is best! "];
// Delete the character string containing "justcoding"
[Str deleteCharactersInRange: [str rangeOfString: @ "justcoding"];
NSLog (@ "str = % @", str );
}
9. String insertion
StringWithString method: used to create a string to initialize the value assignment.
InsertString method: The String object inserted by the first parameter and the position inserted by the second parameter.
C code-(void) viewDidLoad
{
[Super viewDidLoad];
// Create a string
NSMutableString * str = [NSMutableString stringWithString: @ "justcoding is"];
// Insert a string in str 10th characters
[Str insertString: @ "best man" atIndex: 10];
NSLog (@ "str = % @", str );
}
10. Copy strings
C code-(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 does the str1 data change after str2 is added? This is the charm of the pointer, because we operate on the pointer, str2 = str1 means that the two pointers point to a piece of memory at the same time, then the memory content pointed to by str2 will change after str1.
11. String and specified type conversion
If the converted parameter is invalid, no exception will be thrown, for example, converting the integer type in Chinese. No error is reported, but the conversion result is 0. The default value is.
C code-(void) viewDidLoad
{
[Super viewDidLoad];
// Convert string to integer
NSString * str0 = @" 1121 ";
// NSString * str0 = @ "China ";
// Convert string to integer type
Int I = [str0 intValue];
NSLog (@ "converted: % I", I );
// Convert string to interger
NSString * str1 = @" 1985 ";
// NSString * str1 = @ "China ";
// Convert string to interger
NSInteger ii = [str1 integerValue];
NSLog (@ "converted: % I", ii );
// Convert string to double
NSString * str2 = @" 3.145926 ";
// NSString * str2 = @ "China ";
// Convert string to double
Double d = [str2 doubleValue];
NSLog (@ "converted: % f", d );
// Convert string to float
NSString * str3 = @" 3.145926 ";
// NSString * str3 = @ "China ";
// Convert string to float
Double f = [str3 floatValue];
NSLog (@ "converted: % f", f );
}