IOS, ios9
/* ____________________ NSString (immutable string )________________________*/
// ________________________ 1. Create a string ___________________________________//
// Create a String constant
NSString * string1 = @ "Hello"; // The content remains unchanged.
String1 = @ "Hello world ";
// Create a string
NSString * string2 = [[NSString alloc] initWithString: @ "Hello"]; // string2 = hello
NSString * string3 = [[NSString alloc] initWithFormat: @ "Hello % @", string2]; // string3 = hello
NSLog (@ "string2 = % @", string2 );
NSLog (@ "string3 = % @", string3 );
// InitWithFormat can be used to place placeholders (formatting characters ). Splicing function (string, including basic data types)
// Example of initWtihformat usage:
// For example, if a result is queried, all the results are spliced and output.
NSString * s1 = @ "zhangsan ";
NSString * s2 = @ "lisi ";
NSString * s3 = @ "wangwu ";
// Concatenate a string
NSString * string4 = [NSString stringWithFormat: @ "% @, % @, % @", s1, s2, s3];
NSLog (@ "% @", string4 );
// Basic data types can also be spliced (initWithFormat)
Int age = 24;
NSString * string5 = [[NSString alloc] initWithFormat: @ "% @ age: % d", s1, age];
NSLog (@ "string5 =%@", string5 );
// ________________________ 2. Comparison of strings ______________________________________
NSString * string6 = @ "abck ";
NSString * string7 = @" 88888 ";
// Compare whether the two pointer addresses are the same, instead of comparing the content in the string
// If (string6 = string7)
// * Isw.tostring compares whether the content of the two strings is the same. (Case Sensitive)
If ([string6 isw.tostring: string7]) {
NSLog (@ "the same content as string6 and string7 ");
}
NSString * string8 = @ "abc ";
NSString * string9 = @ "abc ";
If (string8 = string9 ){
NSLog (@ "string8 = string9"); // YES
}
// Compare the following
NSString * string10 = [[NSString alloc] initWithFormat: @ "abc % @", @ "def"];
NSString * string11 = [[NSString alloc] initWithFormat: @ "abc % @", @ "def"];
If (string10 = string11 ){
NSLog (@ "string10 = string11"); // NO
}
If ([string10 isdue to: string11]) {
NSLog (@ "string10, the content of string11 is the same"); // YES
}
NSString * string12 = [[NSString alloc] initWithString: @ "abc"];
NSString * string13 = [[NSString alloc] initWithString: @ "abc"];
If (string12 = string13 ){
NSLog (@ "string12 = string13"); // YES reason: iniWithString is not created in the heap, but in the constant area.
}
/*
1. initWithString is not created in the heap, but in the constant area (this is not optimized by the compiler)
2. iniWithFormat: created in the heap.
*/
// __________________ String case-insensitive comparison of caxeInsensitiveCompare ______________
// Compare the size
NSString * string14 = @ "zhangsan ";
NSString * string15 = @ "ZHANGSAN ";
// CaseInsensitiveCompare case-insensitive comparison
// Typedef NS_ENUM (NSInteger, NSComparisonResult) {NSOrderedAscending =-1L, NSOrderedSame, NSOrderedDescending };
//-(NSComparisonResult) caseInsensitiveCompare :( NSString *) string;
NSComparisonResult result = [string14 caseInsensitiveCompare: string15];
If (result = NSOrderedSame ){
NSLog (@ "string14, string15 case insensitive ");
}
NSString * string16 = @ "";
NSString * string17 = @ "B ";
NSComparisonResult result2 = [string16 compare: string17];
If (result2 = NSOrderedAscending ){
NSLog (@ "string16 <string17 ");
} Else if (result2 = NSOrderedSame ){
NSLog (@ "string16 = string17 ");
} Else if (result2 = NSOrderedSame ){
NSLog (@ "string16> string 17 ");
}
// _________________ 3. Other usage of the string ________________
// 3.1 length: obtains the length of a string.
NSString * string18 = @ "abcdef"; // @ "" (Length: 2)
NSInteger len = [string18 length];
NSLog (@ "leng = % ld", len );
// 3.2 rpm, write
NSString * string19 = @ "hElllo ";
// UppercaseString: converts all letters in the string to uppercase.
NSLog (@ "upper: % @", [string19 uppercaseString]);
// Lowercase
NSLog (@ "lower: % @", [string19 lowercaseString]);
// Uppercase letters
NSLog (@ "capitalizedString: % @", [string19 capitalizedString]);
// 3.3 convert string to cost data type
NSString * string20 = @" 3.14 ";
// Error: forced conversion cannot change the object itself
// Float f2 = (float) string20; // error message: Pointer cannot be cast to type 'float'
Float f = [string20 floatValue];
NSLog (@ "floatValue: % f", f );
NSString * string21 = @ "1 ";
Bool B = [string21 boolValue]; // true
NSLog (@ "% I", B );
// Truncate a string of 3.4 (***)
NSString * string22 = @ "abcdef ";
// SubstringToIndex: truncates the string from the start position to the specified position (excluding the character 0 at the specified position ).
NSString * substring1 = [string22 substringToIndex: 3];
NSLog (@ "subtirng1 = % @", substring1 );
// SubstirngFromIndex: truncates a string from a specified position to the end of the string (including characters with a specified position)
NSString * substring2 = [string22 substringFromIndex: 1];
NSLog (@ "subtirng2 =%@", substring2 );
Nsange rang = {1, 4 };
// Nsange rang = NSMakeRange (1, 4); // wait for the rang to be smaller than that of nsange rang = };
NSString * substring3 = [string22 substringWithRange: rang];
NSLog (@ "sustirng3 = % @", substring3 );
// Append the 3.5 string
NSString * string23 = @ "Android ";
// Append @ "ios" to the string string23"
NSString * string24 = [string23 stringByAppendingString: @ "ios"];
NSLog (@ "string24 =%@", string24 );
NSString * string25 = [string23 stringByAppendingFormat: @ "% @ % d", @ "ios", 7];
NSLog (@ "string25 = % @", string25 );
// Query rangeOfString for string 3.6
NSString * string26 = @ "www.iphonetrain.com/ios.html ";
Nsange rg = [string26 rangeOfString: @ "ios"];
// No string to be searched
// NSNotFound definition: enum {NSNotFound = NSIntegerMax}; # define NSIntegerMax LONG_MAX
If (rg. length! = NSNotFound ){
NSLog (@ "location: % ld, length: % ld", rg. location, rg. length );
}