ObjectiveC development tutorial -- basic operations on strings
// 1. NSString
/* ---------------- Method for creating strings ----------------*/
// 1. Create a constant string.
NSString * astring = @ This is a String !;
// 2. Create an empty string and assign a value.
NSString * astring = [[NSString alloc] init];
Astring = @ This is a String !;
[Astring release];
NSLog (@ astring: % @, astring );
// 3. In the preceding method, the speed is improved by using the initWithString method.
NSString * astring = [[NSString alloc] initWithString: @ This is a String!];
NSLog (@ astring: % @, astring );
[Astring release];
// 4. Use Standard c to create a string: initWithCString Method
Char * Cstring = This is a String !;
NSString * astring = [[NSString alloc] initWithCString: Cstring];
NSLog (@ astring: % @, astring );
[Astring release];
// 5. Create a formatted string: A placeholder (consisting of one % and one character)
Int I = 1;
Int j = 2;
NSString * astring = [[NSString alloc] initWithString: [NSString stringWithFormat: @ % d. This is % I string !, I, j];
NSLog (@ astring: % @, astring );
[Astring release];
// 6. Create a temporary string
NSString * astring;
Astring = [NSString stringWithCString: This is a temporary string];
NSLog (@ astring: % @, astring );
/* -------------- Read the string from the file: initWithContentsOfFile method ----------------*/
NSString * path = @ astring. text;
NSString * astring = [[NSString alloc] initWithContentsOfFile: path];
NSLog (@ astring: % @, astring );
[Astring release];
/* ---------------- Write a string to the 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];
/* ---------------- Compare two strings ----------------*/
// Compare the strcmp function with C
Char string1 [] = string !;
Char string2 [] = string !;
If (strcmp (string1, string2) = 0)
{
NSLog (@ 1 );
}
// IsEqualToString Method
NSString * astring01 = @ This is a String !;
NSString * astring02 = @ This is a String !;
BOOL result = [astring01 isw.tostring: astring02];
NSLog (@ result: % d, result );
// 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
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)
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)
// Compare string 1 case-insensitive
NSString * astring01 = @ this is a String !;
NSString * astring02 = @ This is a String !;
BOOL result = [astring01 caseInsensitiveCompare: astring02] = NSOrderedSame;
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)
// Compare string 2 without case sensitivity
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.
/* -------------- 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
/* ------------- Search for the substring ----------------*/
NSString * string1 = @ This is a string;
NSString * string2 = @ string;
Nsange range = [string1 rangeOfString: string2];
Int location = range. location;
Int leight = range. length;
NSString * astring = [[NSString alloc] initWithString: [NSString stringWithFormat: @ Location: % I, Leight: % I, location, leight];
NSLog (@ astring: % @, astring );
[Astring release];
/* ---------------- Extract the substring ----------------*/
//-SubstringToIndex: captures the string from the beginning to the specified position, but does not include the characters at the position.
NSString * string1 = @ This is a string;
NSString * string2 = [string1 substringToIndex: 3];
NSLog (@ string2: % @, string2 );
//-SubstringFromIndex: starts with a specified position (including the characters at the specified position) and includes all subsequent characters.
NSString * string1 = @ This is a string;
NSString * string2 = [string1 substringFromIndex: 3];
NSLog (@ string2: % @, string2 );
//-SubstringWithRange: // captures the substring from the string based on the given position and length.
NSString * string1 = @ This is a string;
NSString * string2 = [string1 substringWithRange: NSMakeRange (0, 4)];
NSLog (@ string2: % @, string2 );
// Extension path
NSString * Path = @~ /NSData.txt;
NSString * absolutePath = [Path stringByExpandingTildeInPath];
NSLog (@ absolutePath: % @, absolutePath );
NSLog (@ Path: % @, [absolutePath stringByAbbreviatingWithTildeInPath]);
// File Extension
NSString * Path = @~ /NSData.txt;
NSLog (@ Extension: % @, [Path pathExtension]);
/*************************************** **************************************** ************
NSMutableString
**************************************** **************************************** ***********/
/* ------------- Allocate capacity to the string ----------------*/
// StringWithCapacity:
NSMutableString * String;
String = [NSMutableString stringWithCapacity: 40];
/* ------------- Add the character ---------------- */after the existing string ----------------*/
// AppendString: and appendFormat:
NSMutableString * String1 = [[NSMutableString alloc] initWithString: @ This is a NSMutableString];
// [String1 appendString: @, I will be adding some character];
[String1 appendFormat: [NSString stringWithFormat: @, I will be adding some character];
NSLog (@ String1: % @, String1 );
*/
/* -------- Delete characters in an existing string according to the given range and length ------*/
/*
// DeleteCharactersInRange:
NSMutableString * String1 = [[NSMutableString alloc] initWithString: @ This is a NSMutableString];
[String1 deleteCharactersInRange: NSMakeRange (0, 5)];
NSLog (@ String1: % @, String1 );
/* -------- Insert the given string in the specified position after the existing string ------*/
//-InsertString: atIndex:
NSMutableString * String1 = [[NSMutableString alloc] initWithString: @ This is a NSMutableString];
[String1 insertString: @ Hi! AtIndex: 0];
NSLog (@ String1: % @, String1 );
/* -------- Replace an existing null character string with another character string ------*/
//-SetString:
NSMutableString * String1 = [[NSMutableString alloc] initWithString: @ This is a NSMutableString];
[String1 setString: @ Hello Word!];
NSLog (@ String1: % @, String1 );
/* -------- Replace the original character with the given range and string ------*/
//-SetString:
NSMutableString * String1 = [[NSMutableString alloc] initWithString: @ This is a NSMutableString];
[String1 replaceCharactersInRange: NSMakeRange (0, 4) withString: @ That];
NSLog (@ String1: % @, String1 );
/* ------------- Determine whether the string contains other strings (prefix, suffix )-------------*/
// 01: Check whether the string starts with another string-(BOOL) hasPrefix: (NSString *) aString;
NSString * String1 = @NSStringInformation.txt;
[String1 hasPrefix: @ NSString] = 1? NSLog (@ YES): NSLog (@ NO );
[String1 hasSuffix: @.txt] = 1? NSLog (@ YES): NSLog (@ NO );